Hi so I am trying to take a cstring and make it lowercase, but when I am printing the cstring at the end I am getting a weird format box where some of the letters should be. Do anyone have any ideas?
#include <string>
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int i=0;
char* str="TEST";
char c;
char* cstr = new char[strlen(str) + 1];
while (str[i])
{
c = str[i];
c = tolower(c);
strcat(cstr, &c);
i++;
}
cout << cstr << endl;
return 0;
}
The problem is that you are calling strcat
incorrectly. The second parameter is not a null-terminated string.
You really don't need to call strcat
at all. Just write directly to the output string:
Try:
while (str[i])
{
c = str[i];
c = tolower(c);
cstr[i] = c;
i++;
}
cstr[i] = 0;
or, equivalently:
while(str[i])
{
cstr[i] = tolower(str[i]);
i++;
}
cstr[i] = 0;