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.

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure about "< C11"? I think C99 is enough.
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);
}

6 Comments

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, you can use printf("The string: '%s'\n", String); to print the contents of the string and verify that the outputs match.
I have just used that and I can confirm it will only show the first 3 characters
@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?
Unfortunately not I'm using a block of characters eg. egwharewh
|
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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.