I'm pretty new to C and am hitting a wall when creating the below function. I want to use this function to make the first letter of a word upper case for a static character array (char string[]. It looks ok to my eye, but I'm getting some syntax errors which are probably pretty basic. compiler errors:
error: invalid conversion from
const char' to
const char*' initializing argument 1 of `size_t strlen(const char*)' assignment of read-only location
void Cap(char string[]){
int i;
int x = strlen(string);
for (i=1;i<x;i++){
if (isalpha(string[i]) && string[i-1] == ' '){
// only first letters of a word.
string[i]= toupper(string[i]);
}if (isalpha(string[0]))
{
string[0]=toupper(string[0]);
}
}
}
string[i]= toupper(string[i])
doesn't complain due to theconst
nature of thestring
parameter, get a new compiler. That param should bechar string[]
. And you wantstrlen(string)
, notstrlen(string[i])
. And lose thereturn Cap
line completely. This is avoid
function. - WhozCraig