-2

I am trying to make every first word letter capital, but it ignores the first word and jumps to second. "apple macbook" should be "Apple Macbook", but it gives me "apple Macbook". If I add printf(" %c", toupper(string[0])); before for loop and change p=1 in for loop it gives me correct result, but if string starts with a space then it will fail. Here is the code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char string[] = "apple macbook";
    int p;
    for(p = 0; p<strlen(string); p++)
    {
        if(string[p] == ' ')
        {
            printf(" %c", toupper(string[p+1]));
            p++;
        }
        else
        {
            printf("%c", string[p]);
        }
    }
    return 0;
}
3
  • Obviously the code doesn't work because the very first letter in the string is a special case with no space before it. You have to treat that special case separately. Also, you need to iterate from 0 to strlen(string)-1, otherwise your program will always destroy the null terminator and then crash when a space is the last letter of the string.
    – Lundin
    Commented Nov 30, 2015 at 12:36
  • 2
    Are you surprised? There is no space before "Apple".
    – M Oehm
    Commented Nov 30, 2015 at 12:36
  • You test for a whitespace character in your if statement. The next character will be converted to upper case. Try if (p==0 || string[p] == ' ') Commented Nov 30, 2015 at 12:36

2 Answers 2

5

A simple work around can be as follows:

 for(p = 0; p<strlen(string); p++)
    {
        if(p == 0 || string[p - 1] == ' ')
        {
            printf("%c", toupper(string[p]));
        }
        else
        {
            printf("%c", string[p]);
        }
    }
9
  • @gsamaras: You say that as if changing the input string were such a good idea... :-)
    – M Oehm
    Commented Nov 30, 2015 at 12:37
  • Hmmm @MOehm you are right, I will update my question!
    – gsamaras
    Commented Nov 30, 2015 at 12:38
  • 1
    @xxxvodnikxxx If p is zero, it will not go for next condition, ie, string[p - 1] == ' '
    – niyasc
    Commented Nov 30, 2015 at 12:40
  • 2
    @xxxvodnikxxx: C's short-circuit evaluation guarantees that the expression right of the logical or || isn't evaluated when the left expression is true, which means the the whole expression must be true.
    – M Oehm
    Commented Nov 30, 2015 at 12:40
  • 1
    @xxxvodnikxxx: Not even potentially. This is guaranteed and a lot of code relies on it.
    – M Oehm
    Commented Nov 30, 2015 at 12:41
0

Change this:

char string[] = "apple macbook";

to this:

char string[] = " apple macbook";

and you will get what you want.

The reason is that in your loop, you search for a space to change the letter afterwards.


However, niyasc's answer is better, since it does not alter the input string but the logic of your program.

I mostly did that to exploit the reason that you are getting the behaviour you encountered, so that you were urged to change your logic by yourself. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.