Search code examples
cfor-loopmultidimensional-arraynested-loopsc-strings

Take string input until it is equal to "/0"


I want the user to enter a maximum of R strings either until the loop ends or until they hit ENTER so basically the input would be equal to "\0". Then I want to count the amount of characters that have been entered by the user.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define R 5
#define C 2


int main(){

    char Pin[R][C];

    char str[100];

    for(int i = 0; i < R; i++){
        
            printf("Enter a string: ");
            scanf("%s", &Pin[i]);
            if(str == "\0"){break;}
            strcpy(Pin[i],str);
    }
    int sum = 0;
    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){

            int kapa = sizeof Pin[i];
            sum += kapa;
        }
    }

    printf("\n\n%d", sum);

    
    return 0;
}

This is my code, but whenever i hit enter to get another input it will skip once. Another problem is that it counts the character "\n" as well and i'd prefer it if it wouldnt.


Solution

  • The presented program does not make a sense.

    For example each character array Pin[i] can contain a string with at most one character if not to count the terminating zero character '\0' of string.

    Within this for loop

    for(int i = 0; i < R; i++){
        
            printf("Enter a string: ");
            scanf("%s", &Pin[i]);
            if(str == "\0"){break;}
            strcpy(Pin[i],str);
    }
    

    there is used uninitialized array str that is not changed within the loop.

    This if statement

    if(str == "\0"){break;}
    

    is always evaluates to false because there are compared two pointers of first characters of the array str and of the string literal "\0". As they occupy different extents of memory then their addresses are different.

    Also the information about how many strings were entered stored nowhere.

    These nested for loops

    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
    
            int kapa = sizeof Pin[i];
            sum += kapa;
        }
    }
    

    also do not make a sense. For example the index j is not used. The expression sizeof Pin is a constant expression and always evaluates to 2 by the definition of the array Pin. You need to use the function strlen instead of the operator sizeof.

    It seems what you mean is the following

    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
        enum { R = 5, C = 100 };
        char Pin[R][C];
    
        size_t n = 0;
    
        for (; n < R; n++)
        {
            char s[C];
            s[0] = '\0';
    
            printf( "Enter a string: " );
    
            if ( !fgets( s, sizeof( s ), stdin ) || s[0] == '\n') break;
    
            s[strcspn( s, "\n" )] = '\0';
    
            strcpy( Pin[n], s );
        }
    
        size_t sum = 0;
        for (size_t i = 0; i < n; i++)
        {
            sum += strlen( Pin[i] );
        }
    
        printf( "\nsum of lengths of the strings is %zu\n", sum );
    }
    

    The program output might look like

    Enter a string: 1
    Enter a string: 12
    Enter a string: 123
    Enter a string:
    
    sum of lengths of the strings is 6