-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;
}
ChrisMe
  • 55
  • 5
  • 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 Nov 30 '15 at 12:36
  • 2
    Are you surprised? There is no space before "Apple". – M Oehm Nov 30 '15 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] == ' ')` – Peter Paul Kiefer Nov 30 '15 at 12:36

2 Answers2

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]);
        }
    }
niyasc
  • 4,440
  • 1
  • 23
  • 50
  • @gsamaras: You say that as if changing the input string were such a good idea... `:-)` – M Oehm Nov 30 '15 at 12:37
  • Hmmm @MOehm you are right, I will update my question! – gsamaras Nov 30 '15 at 12:38
  • I guess there can be problem with potencially acces to string[p-1] while p will be 0, can't ? (Of course, in fact there is fixed by first decision, but .. ) Or char[-1] will return '\0' as "null char" ? – xxxvodnikxxx Nov 30 '15 at 12:38
  • 1
    @xxxvodnikxxx If p is zero, it will not go for next condition, ie, `string[p - 1] == ' '` – niyasc Nov 30 '15 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 Nov 30 '15 at 12:40
  • 1
    @xxxvodnikxxx: Not even potentially. This is guaranteed and a lot of code relies on it. – M Oehm Nov 30 '15 at 12:41
  • Ok guys :D :D .. And btw theoretically what will return "1 position before zero index" ? will be an exception, null, or something else? :) – xxxvodnikxxx Nov 30 '15 at 12:42
  • @xxxvodnikxxx: That would be an array access out of bounds, which leads to undefined behaviour. But the presented code is well-behaved. – M Oehm Nov 30 '15 at 12:43
  • @xxxvodnikxxx: that's undefined, so it's best to check before you do so and prevent doing this if `index` may be zero. – Jongware Nov 30 '15 at 12:43
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. :)

gsamaras
  • 71,951
  • 46
  • 188
  • 305