简体   繁体   中英

Capitalize first letter in string in c

I have a problem. This function that I have needs to clean a string of any non alphabetical characters, while lowercasing all of the alphabetic characters. Using pointers, p_fast checks to see if the character in the string is isalpha ; if it is, the character is stored into p_slow . After doing this in the whole string, a \\0 character is added to complete the string. After this, I need to capitalize the first letter in the string that was just cleaned and lowercased.

/**********************************************************************/
/*                      Clean up customer names                       */
/**********************************************************************/
void clean_names(int quantity,
                               struct customer *p_customer_records_start)
{
struct customer *p_customer;
char *p_fast = p_customer_records_start->customer_name,
*p_slow = p_customer_records_start->customer_name;


for(p_customer = p_customer_records_start;
     (p_customer-p_customer_records_start) < quantity; p_customer++)
{
    p_fast = p_customer->customer_name;
    p_slow = p_customer->customer_name;

while (*p_fast != END_OF_STRING)
{
    if(isalpha(*p_fast))
        *p_slow++ = tolower(*p_fast);
    p_fast++;
}

*p_slow = END_OF_STRING;

}
return;
}

I don't know how to get back to the beginning of the string. I can't find anything on the internet. If anyone could help, that would be great! If you need more information, just ask.

You started with the pointer in p_customer->customer_name . In the end, that pointer still points in the same location (to the beginning of the name). You can use it to capitalize the first letter.

Very simple: toupper only works on a single char per call. If you do something like this :

p_customer->customer_name[0]=toupper(p_customer->customer_name[0]);

It capitalizes only the first letter.

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