简体   繁体   中英

Replacing one letter with another in C

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM