0

I'm very new to programming and decided to start with C. This is my first real problem where I can't figure it out for myself.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
        char String[20];
        int CharNo = 0;

        //Asking the User to input some characters to use in the program
        printf("Enter a few characters please:\n");
        scanf("%s", String);

        printf("%c\n", String[0]);

        return 0;
    }

I have asked the user to input a few random characters, then what I want to be able to do is print out the input string character by character on a new line. All I'm able to do by myself if print out one of the characters by itself.

Any help about this problem and my coding overall would be greatly appreciated.

Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52
Tom
  • 1
  • 2
  • You need a `for` loop. Here's a [list of books](http://stackoverflow.com/a/562377/3386109) to help you get started. – user3386109 Oct 28 '15 at 19:00
  • Possible duplicate of [How to input a string using scanf in c including whitespaces](http://stackoverflow.com/questions/5054414/how-to-input-a-string-using-scanf-in-c-including-whitespaces) – user23573 Oct 28 '15 at 19:52

3 Answers3

2

Use a for loop!

for (size_t i = 0; i < string_length; ++i) {
     /* print ith character of the string */
}

or, if you're still < C991, define size_t i; before the loop and initialize it with i = 0.

Notes:

  • use scanf("%19s", String) instead to preempt buffer overflows. 19 to leave room for the terminating null byte
  • in C, variabels commonly have lowercase identifiers like string or charNo. How to handle concatentation of multiple words in one identifier is up to you. Common choices are first_second and firstSecond

1 as @szczurcio noted in the comments to this answer

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
1

Approach 1

  1. Get the length of the string
  2. Use a loop to iterate over the length of the string.
  3. In each iteration of the loop, print one character.
int len = strlen(String);
for (int i = 0; i < len; ++i )
{
   printf("%c\n", String[i]);
}

Approach 2

  1. Create a pointer that points to the string.
  2. Use a loop in which you increment the pointer until it points to the null character.
  3. In each iteration of the loop print the character that the pointer points to.
char* cp = String;
for ( ; *cp != '\0'; ++cp )
{
   printf("%c\n", *cp);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you for the quick response, I used your first approach and it does print characters on new lines but it only does the first 3 characters and im completely clueless as to why. – Tom Oct 28 '15 at 20:45
  • @Tom, you can use `printf("The string: '%s'\n", String);` to print the contents of the string and verify that the outputs match. – R Sahu Oct 28 '15 at 20:47
  • I have just used that and I can confirm it will only show the first 3 characters – Tom Oct 28 '15 at 21:43
  • @Tom, When you use '"%s"' as the format specifier for `scanf`, it reads only non-whitespace characters. Could it be that your input consists of the three non-whitespace characters followed by a whitespace character? – R Sahu Oct 28 '15 at 21:47
  • Unfortunately not I'm using a block of characters eg. egwharewh – Tom Oct 28 '15 at 21:58
  • @Tom, That is strange indeed. – R Sahu Oct 28 '15 at 21:59
0
    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
        char String[20];
        int CharNo = 0;
        char *c;

        printf("Enter a few characters please:\n");
        scanf("%s", String);

        c=String;
        while(*c) putchar(*c++);      

        return 0;
    }
V. Michel
  • 1,599
  • 12
  • 14