0
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>    

int main (void){

    string name = GetString();
    printf("%c", toupper (name[0]));

    for (int i = 0 ; i < strlen(name) ; i++)
    {
        if(i == '\0'){
           printf("%c", toupper (name[i+1]));
    }
  }
    printf("\n");
}

Everytime I run my code above I only get the first 2 characters of my input captalized:

./initials

Jake Steevens

JA

Shouldn't the for loop go through every character typed and, after it finds the '\0' (space), print out the next character?

2
  • Try and use: " " to express a space there. The '\0' symbol (that represents the end of a word, for example) should only be a used if i+1 is a condition as well.
    – Gordon
    Feb 10 '17 at 20:00
  • I replaced ' for " as you said, but it only gave me errors: initials.c:13:14: error: result of comparison against a string literal is unspecified (use strncmp instead) [-Werror,-Wstring-compare] if(i == "\0"){ ^ ~~~~ initials.c:13:14: error: comparison between pointer and integer ('int' and 'char *') [-Werror] if(i == "\0"){ ~ ^ ~~~~ 2 errors generated.
    – rotciv
    Feb 10 '17 at 20:36
1

Gordon is right \0 or nul is just 00000000 i believe but the space character has a asc|| binary of 00100000 so your searching for the wrong character. I did the same. i would save an ascii web page to your bookmark bar for assistance if you already haven't

6
  • if (name[i] == ' ' && name[i+1] != '\0') is what's missing here. After all, you've already understood the logic of the print message that comes after it, think of it like that.
    – Gordon
    Feb 11 '17 at 5:40
  • I see now, so space is not the same as null. For further reference, if anyone is wondering the difference between null and space, @Mark Byers explains it quite well here: stackoverflow.com/questions/3696298/…
    – rotciv
    Feb 12 '17 at 14:05
  • Thanks for the help guys :D
    – rotciv
    Feb 12 '17 at 14:09
  • @Gordon, that if statement wouldn't run correctly, if I'm not mistaken. When I try to use something similar, it throws me an error for "result of comparison against a string literal is unspecified" and tells me to use strncmp instead. Feb 13 '17 at 13:12
  • That's probably because you have to set another variable against the strlen of [name], since if you think about it- i can't serve as both of those variables in the for loop...
    – Gordon
    Feb 14 '17 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .