0

having a hard time with this problem set. no problem in getting first letter, it's the letters that follow a space that are giving me a hard time. any hints appreciated.

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

int main(void)
{
   //prompt user for name
   string s = get_string();
   if (s != NULL)
   {
        //first letter
        for(int i = 0, n = strlen(s); i < n; i++)
        {
            if (i < 1)
            {
                printf("%c", toupper(s[i]));
                i++;
            }
            // for letter after space?
            for (;s[i] != ' '; i++); //receiving seg fault in this line
            i++;
            printf("%c", toupper(s[i]));
        }
    }
    printf("\n");
}
2

You should print a character s[i] if it's not space, and either the first character, or preceded by a s[i-1] being a space. You can put that into a single combined condition, and have only one output command in the loop. Don't compare index i and space, but the character s[i] and space.

0

You must log in to answer this question.

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