The function tolower
returns an integer and you are wrongly casting it to char *
.
The best way to do this is to copy alphanumeric characters from the source array into the destination array
char myline[] = " 50 Some Text Here "; // put the [] after the variable.
char *mystr = malloc(128);
char *tmp = myline;
char *destPtr = mystr;
while (*tmp != '\0'){
if (isalnum(*tmp)) {
*destPtr++ = *tmp;
}
tmp++;
}
*destPtr = 0; // terminating nul character.
If you really want to use strcpy
, you need to initialize your destination string to an empty string and make the character to be copied part of a character array and append that array to the destination string:
char myline[] = " 50 Some Text Here "; // put the [] after the variable.
char *mystr = malloc(128);
char *tmp = myline;
mystr[0] = 0; // initialize the destination string.
while (*tmp != '\0'){
char str[2] = {0}; // temp string of size 2.
if (isalnum(*tmp))
str[0] = tolower(*tmp); // put the char to be copied into str.
strcat(mystr,str); // append.
tmp++;
}