Jesse Upchurch
Jesse Upchurch

Reputation: 91

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.

Upvotes: 0

Views: 678

Answers (2)

ryyker
ryyker

Reputation: 23226

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.

Upvotes: 0

sth
sth

Reputation: 229754

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.

Upvotes: 2

Related Questions