[Solved] ‘ ‘, ‘\n’ , scanf() and output screen


Here’s a working code:

#include <stdio.h>

int main ()
{
        char x[100] = {0};
        char m;
        int i, j;

        printf ("Type:\n");
        for (i=0; i<99; i++) {
                m = getchar();
                getchar();
                if ((m != ' ') && (m != '\n')) {
                        x[i] = m;
                } else {
                        printf ("Breaking.");
                        break;
                }
        }

        printf ("\n");
        for (j=0; j<i; j++)
                printf ("%c\n", x[j]);

        return 0;
}

Here I have used an extra getchar() to consume the newline used to enter the character m. And so (please note) you will also need to enter a newline after a space (‘ ‘) and a newline (‘\n’) to enter and store these in m and compare them in the next lines.

solved ‘ ‘, ‘\n’ , scanf() and output screen