Home > OS >  How can I add \n the first time I read a char in C?
How can I add \n the first time I read a char in C?

Time:10-27

I am new trying to learn C programming, I code a little program that read only the integers from an input with both integers and characters but I want to go a little further: I need to add a \n char only the first time a character is read from the input (in this case the standard input but it could be a file). This is the code I can get:

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

#define M 1000

int main(void)
{
    char a;
    char enteros[M];
    int output[M];
    int count = 0;
    int i;
    int x = 0;
    int index = 0;
    int num = 0;
    char *str;

    while (fscanf(stdin, "%c", &a) != EOF)
    {
        enteros[count] = a;
        count  ;
    }

    enteros[count] = '\0';
    str = enteros;

    while (*str)
    {
        if (sscanf(str, "%d%n", &num, &x) == 1)
        {
            output[index] = num;
            index  ;
        }

        str  = x;

        for (; *str; str  )
        {
            if (*str >= '0' && *str <= '9') /* positive value */
                break;
            if (*str == '-' && *(str   1) >= '0' && *(str   1) <= '9') /* negative */
                break;
        }
    }

    for (i = 0; i < index; i  )
    {
        printf(" %d", output[i]);
    }

    return 0;
}

When the input is:

1 8 4000 2 -23 end 51 87 end –4
–3 2 end

The output should be:

1 8 4000 2 -23 
51 87 
-4 -3 2

But instead I get:

1 8 4000 2 -23 51 87 4 3 2

Thanks for the help!

CodePudding user response:

Add a flag variable to control when a newline should be printed.
Set the flag when an integer has been scanned and printed.
Reset the flag when a newline is printed.
ctypes.h has isalpha that can be used to detect letters.
For this example the input is hard coded rather than taken from stdin.

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

#define M 1000

int main(void)
{
    char a;
    char enteros[M];
    int output[M];
    int newline = 0;
    int count = 0;
    int i;
    int x = 0;
    int index = 0;
    int num = 0;
    char *str;

    // while (fscanf(stdin, "%c", &a) != EOF)
    // {
        // enteros[count] = a;
        // count  ;
    // }
    // enteros[count] = '\0';

    strcpy ( enteros, "1 8 4000 2 -23 end 51 87 end -4 -3 2 end");

    str = enteros;

    while (*str)
    {
        x = 1;
        if (sscanf(str, "%d%n", &num, &x) == 1)
        {
            output[index] = num;
            printf(" %d", output[index]);
            newline = 0;
            index  ;
        }

        str  = x;

        for (; *str; str  )
        {
            if ( *str == '-' || (*str >= '0' && *str <= '9')) {
                break;
            }
            else if ( ! newline && isalpha ( (unsigned int)*str)) {
                printf ( "\n");
                newline = 1;
            }
        }
    }

    // for (i = 0; i < index; i  )
    // {
        // printf(" %d", output[i]);
    // }
    printf ( "\n");

    return 0;
}

CodePudding user response:

You can write a newline character, as follows:

printf "\n"
  •  Tags:  
  • c
  • Related