Search code examples
carrayscharfillfunction-definition

How to fill a char array with elements, if you press Enter (on keyboard) it should end FOR loop and print the whole array till Enter


Not a totally beginner in C. Yesterday i got task like i said in the title to input elements in array till pressing ENTER. If you press it'll break it. There is more that is given in the task. But the rest of it was easy to write. So this half of code is making me a trouble. Basically i know how to break if i enter . or any other character from ASCII table but how if it's ENTER or DELETE or similar like that. If you can provide a code for the solution. I would be grateful.

EDIT: Tried with \n, \r, even converted array in int and compared with 10 in ASCII table which represents LINE FEED and also tried with getchar(), nothing works.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX 50

void main() {
    char array_org1[MAX], array_org2[MAX];
    int size1=0,size2=0;
    printf("\nEnter the values of the first array:\n");

    for (int i = 0; i < MAX; i++) {


        printf("element[%d] = ", i);
        scanf(" %c", &array_org1[i], sizeof(array_org1));

        if (array_org1[i] == '.')
        {
            break;
        }
        else
        {
            size1++;
        }

    }   

    printf("\nEnter the values of the second array :\n");

    for (int i = 0; i < MAX; i++) {

        printf("element[%d] = ", i);
        scanf(" %c", & array_org2[i], sizeof( array_org2));
        if (array_org2[i] == '.')
        {
            break;
        }
        else
        {
            size2++;
        }

    }
    printf("\n");

    printf("First array:");
    for (int i = 0; i < size1; i++) {

        printf("%c ",  array_org1[i]);

    }
   printf("\nSecond array:");
    for (int i = 0; i < size2; i++) {

        printf("%c ", array_org2[i]);

    }

    printf("\n");   
}

Here is the result of the code above


Solution

  • I think that you are required ro write a function similar to the following.

    size_t fill( char *s, size_t n )
    {
        size_t i = 0;
    
        for ( int c; i < n && ( c = getchar() ) != EOF && c != '\n'; i++ )
        {
            s[i] = c;
        }
    
        return i;
    }
    

    Here is a demonstrative program

    #include <stdio.h>
    
    #define MAX 50
    
    size_t fill( char *s, size_t n )
    {
        size_t i = 0;
    
        for ( int c; i < n && ( c = getchar() ) != EOF && c != '\n'; i++ )
        {
            s[i] = c;
        }
    
        return i;
    }
    
    int main(void) 
    {
        char array_org1[MAX], array_org2[MAX];
        size_t size1 = 0, size2 = 0;
    
        printf( "Enter the values of the first array: " );
    
        size1 = fill( array_org1, MAX );
    
        printf( "\nEnter the values of the second array: " );
    
        size2 = fill( array_org2, MAX );
    
        printf( "\nFirst array: " );
        for ( size_t i = 0; i < size1; i++ ) 
        {
            putchar( array_org1[i] );
        }
    
        putchar( '\n' );
    
        printf( "Second array: " );
        for ( size_t i = 0; i < size2; i++ ) 
        {
            putchar( array_org2[i] );
        }
    
        putchar( '\n' );
    
        return 0;
    }
    

    Its output might look like

    Enter the values of the first array: Hello
    
    Enter the values of the second array: World!
    
    First array: Hello
    Second array: World!
    

    If the filled array shall contain a string then the function can look like

    size_t fill( char *s, size_t n )
    {
        size_t i = 0;
    
        for ( int c; i + 1 < n && ( c = getchar() ) != EOF && c != '\n'; i++ )
        {
            s[i] = c;
        }
    
        s[i] = '\0';
    
        return i;
    }