0

I'm having some trouble getting started with the initials problem. Currently, if the user inputs: John Smith, only J is printed. I'm assuming the program automatically exits after the first print command? How can I change the logic so my program prints more than the first character? Thanks so much for your help!

int main (void)
{
int i;
char first_letter;
printf("Type your full name: \n");
string name = get_string();

//iterate over each character in string
for (i = 0; i < strlen(name); i ++) {
first_letter = name[0];
} //end for loop

printf("%c", first_letter);

if (name[i] == ' ') {
    printf("%c\n", name[i + 1]);
}
} //end main
2

The only problem is that your second IF condition is outside the For loop:

if (name[i] == ' ') {
    printf("%c\n", name[i + 1]);

so that the blank space is never reached, the solution would be to introduce it into the loop. On the other hand the statement:

first_letter = name[0];

I would take it out of the loop, since you know what the index is, you do not need a loop.

You must log in to answer this question.

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