0

I constructed a for loop with a condition inside of it to check for spaces and print out the character after the space. However when I run this, it only prints out the first letter of the name and it seems like the for loop is failing at printing anything.

#include <string.h>

int main(void)
{
    printf ("What is your name? (must be less than 30 characters)\n");
    char N[30];
    scanf ("%s", N);
    printf ("%c",*N);
    for (int i=0; i<30; i++)
    {
        if (N[i]==' ')
        {
            printf ("%c\n", N[i+1]);
        }
    }
}

this is and examplehow it currently runs:

input: thomas roberts

output: t

desired output: tr

Can anyone offer any insight as to how I've screwed up?

1
  • I think the way your scanf statement works, you're only getting the first name. Using %s reads up until the first white space. So for your example, N will contain "thomas\0". Here's a complicated solution: stackoverflow.com/questions/1247989/…, but most people in cs50, I think are just using getString().
    – Sam Gerber
    Oct 1, 2015 at 16:56

1 Answer 1

0

As @SamGerber has mentioned, you have two obvious mistakes.

  1. You don't include the stdio.h library and so you can't use the printf() function that it includes.
  2. scanf() stands for scan formatted so the string you are supposedly reading is just a sequence of chars without any white-space in between, something that is not true in our case. See the specification for this pset:

Write, in a file called initials.c, a program that prompts a user for their name (using GetString to obtain their name as a string) and then outputs their initials in uppercase with no spaces or periods, followed by a newline (\n) and nothing more. You may assume that the user’s input will contain only letters (uppercase and/or lowercase) plus single spaces between words. Folks like Joseph Gordon-Levitt, Conan O’Brien, and David J. Malan won’t be using your program.

So there is no specification for the number of space separated names you are gonna be given. Could be one, could be two, could be three or even twenty!

So better use GetString() as described by the spec and not scanf(). It surly is possible to implement it using scanf(), but it's not what you are asked for. You should learn to follow the instructions you are given. Even if you implemented the scanf() correctly, you might not get a full grade because you haven't implement it the way it was supposed to be implemented.

  1. Aside from that, your for loop is also wrong. You shouldn't traverse all the 30 chars (even though it's again not specified that the name is going to be less than 30 chars). You should instead have used strlen() and traverse only until the end of the string.

Hope this helps you get going!


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

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