0

I want this code to skip through a name like " bob dylan", ignore the spaces before "bob" and print "B".

It is not printing anything for some reason.

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

int main(void)
{


    printf("input name: ");
    string name = GetString();


    int start = 1;
    while(strcmp(&name[start], " ") == 0)
    {
        start++;
        printf("%c\n", toupper(name[start]));
    }
    start++;
}

1 Answer 1

1
while(strcmp(&name[start], " ") == 0)

Let's suppose: string name = bob dylan then &name[start] = ob dylan since you do start = 1 this will never be equal to " ", unless you enter two spaces with GetString, is much better to use a for loop, to go all the array and impose an IF condition that is true when we found a space, and then print the next character after the space

You must log in to answer this question.

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