Reputation: 2612
I am fooling around with C for fun. My program prompts a user to enter a word that they would like defined. Then my program uses CURL plus a dictionary API to return the definition. My problem is that the definition isn't formatted properly, so I would like to do that. That leads to my question.
I need to capitalize the first word of the sentence. The definition is in char* format. I am not sure which C string functions to use.
What I have done so far is copy the first character of the definition into its own char variable. Then using toupper() I converted it to upper case. I am not sure how I can replace the lowercase letter in the definition string with my new upper case letter.
Here is some code.
char upperCase;
strncpy(&upperCase, r, 1); //copy first char of definition to upperCase (to be converted to uppercase)
printf("%c\n", toupper(upperCase)); //just prints the uppercase letter to make sure it works
printf("%s\n", r); //print the definition
r is the string with the definition.
Upvotes: 0
Views: 440
Reputation: 51920
You can work directly on the character inside the string:
r[0] = toupper(r[0]);
You can do this because the expression r[0]
is of type char
. Also note that you can use array syntax on pointers. If r
is a char*
, you can still treat it as an array and refer to its individual char
contents with r[index]
. r[0]
for the first character in the string, r[1]
for the second, and so on.
Upvotes: 4