Search code examples
ccharc-stringsuppercaseletter

Putting uppercase letters taken from user and putting into an array


In this code im trying to take a string from user and put the uppercase letters into a other array then print it. But im doing something horribly wrong i can tell, can someone help me with this code?

#include<string.h>

int main() {
   char arr[100],uppercaseletters[50];
   printf("Enter array:");
   scanf("%s",arr);
   strlen(arr);
   printf("The lenght of string is: %d\n",strlen(arr));
   for(int i=0;i<=strlen(arr);i++){
    if(arr[i]>='A' && arr[i]<='Z'){
        arr[i]+=uppercaseletters[50];
        printf("%s",uppercaseletters);
       }





   }

}


Solution

  • For starters according to the C Standard the function main without parameters shall be declared like

    int main( void )
    

    It is unclear why the array uppercaseletters has less elements than the array arr.

    char arr[100],uppercaseletters[50];
    

    The user can enter a string consisting only from upper case characters.

    This statement

    strlen(arr);
    

    does not have an effect.

    This statement

    arr[i]+=uppercaseletters[50];
    

    does not make sense. You have to fill the array uppercaseletters. Moreover the element uppercaseletters[50] does not exist because the valid range of indices is [0, 50).

    In this statement

    printf("%s",uppercaseletters);
    

    you are trying to output a non-initialized array.

    The program can look the following way.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void) 
    {
        enum { N = 100 };
        char s[N];
    
        printf( "Enter a string: " );
    
        fgets( s, sizeof( s ), stdin );
    
        size_t n = 0;
    
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( isupper( ( unsigned char )s[i] ) ) ++n;
        }
    
        char *upper_case_letters = malloc( n + 1 );
    
        n = 0;
    
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( isupper( ( unsigned char )s[i] ) )
            {
                upper_case_letters[n++] = s[i];
            }
        }
    
        upper_case_letters[n] = '\0';
    
        puts( upper_case_letters );
    
        free( upper_case_letters );
    
        return 0;
    }
    

    The program output might look for example like

    Enter a string: Hello World!
    HW
    

    Without dynamically allocating an array for upper case letters the program can look the following way.

    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) 
    {
        enum { N = 100 };
        char s[N];
        char upper_case_letters[N];
    
        printf( "Enter a string: " );
    
        fgets( s, sizeof( s ), stdin );
    
        size_t n = 0;
    
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( isupper( ( unsigned char )s[i] ) )
            {
                upper_case_letters[n++] = s[i];
            }
        }
    
        upper_case_letters[n] = '\0';
    
        puts( upper_case_letters );
    
        return 0;
    }