-3

I'm trying to fetch individual characters from user input for a char array, print the input as a string, and then print every individual element as they were entered. Here is my code:

#include <stdio.h>

int main(void)
{
    char string[9];

    int i;
    int counter1 = 0;
    int counter2 = 0;

    for (i=0; i<=10; i++)
    {
        printf("Enter character for element %d: ", counter1);
        scanf("%c\n", &string[counter1]);
        counter1++;
    }
    printf("Your input: %s", string);
    printf("\nArray values:\n");

    while (counter2<=9)
    {
        printf("Element %d: %c\n", counter2, string[counter2]);
        counter2++;
    }
}

Here is the output:

Enter character for element 0: w
w
Enter character for element 1: w
Enter character for element 2: w
Enter character for element 3: w
Enter character for element 4: w
Enter character for element 5: w
Enter character for element 6: w
Enter character for element 7: w
Enter character for element 8: w
Enter character for element 9: w
Enter character for element 10: w
Your input: wwwwwwwwwww�tI�(�)���Array values:
Element 0: w
Element 1: w
Element 2: w
Element 3: w
Element 4: w
Element 5: w
Element 6: w
Element 7: w
Element 8: w
Element 9: w
  • The loop `for (i=0; i<=10; i++)` will iterate over *eleven* elements of your nine-element array. And the loop `while (counter2<=9)` will iterate over *ten* elements of your (still) nine-element array. – Some programmer dude Aug 23 '22 at 09:24
  • 4
    It seems you forgot to include a question in your question. But it looks like you’ve misunderstood C strings, especially null-termination. – Biffen Aug 23 '22 at 09:25
  • 1
    Also remember that all strings in C are *null-terminated* (so a string of ten characters needs space for eleven to fit the terminating `'\0'` character). – Some programmer dude Aug 23 '22 at 09:25
  • Also `scanf("%c\n", ...)` is almost *never* right. Don't have a trailing space (and newline is considered a space character) in your `scanf` formats. See e.g. [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string?noredirect=1&lq=1) – Some programmer dude Aug 23 '22 at 09:27

2 Answers2

0

You're using both 9 and 10 and hoping it will all work out.

#include <stdio.h>

#define LEN 10 // Clearly visible dimension

int main( void ) {
    char string[ LEN + 1 ]; // Show you know about the required '\0'

    int i;
    int counter1 = 0;

    for( i = 0; i < LEN; i++ ) {
        printf("Enter character # %d: ", i );
        scanf( "%c", &string[ i ] ); // holding my nose...
        counter1++;
    }

    string[ i ] = '\0'; // transform char array into "C-style string"

    printf( "Your input: '%s'\n\n", string ); // apostrophes to highlight string

    printf("\nArray values:\n");
    for( i = 0; i < counter1; i++ ) { // Notice upper limit of loop?
        printf( "Element %d: %c\n", i, string[ i ] );
    }
}
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • Thanks so much, I'm busy learning C and using the macro makes sense. A question though, when running this code, after the first prompt to enter a character, the 2nd and 3rd prompts appear next to each other without asking for the 3rd input. – Will Gordge Aug 23 '22 at 09:51
  • SO is full of people with problems using `scanf`... Move to `fgetc( )` or `fgets()` and leave scanf in the gutter... It only seems to bring heartache... – Fe2O3 Aug 23 '22 at 09:53
-1

the char array string has size 9 and you should make it bigger (some thing like 20 or more).