Search code examples
carraysstringuppercaselowercase

How to count the characters of a string and assign them to different groups (lower case & upper case) in C


I wrote a program in C that gets a string from the user (50 characters is the limit) and assign the upper-case characters to a string named upper, and the lower-case characters to lower, in the end it supposed to prints those strings (the upper first). My problem is when I enter a string, it only prints one string (i.e if the string starts with an upper character then upper will be printed) instead of two of them.

Here's my code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define MAX_LEN 50

int main()
{
    char str[MAX_LEN] = { 0 };
    char upper[MAX_LEN] = { 0 };
    char lower[MAX_LEN] = { 0 };
    int i = 0;
    int j = 0;

    printf("Enter a string: ");
    fgets(str, MAX_LEN, stdin);
    str[strcspn(str, "\n")] = 0;

    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] > 'A' && str[i] < 'Z')
        {
            upper[j] = str[i];
        }
        else if (str[i] > 'a' && str[i] < 'z')
        {
            lower[j] = str[i];
        }
        j++;
    }

    printf("%s", upper);
    printf("%s", lower);

    getch();
    return 0;
}

Solution

  • You use one counter for two arrays. You increment the counter, no matter which array you fill in. As a result, the first letter to be processed will determine which array will not have its first character as the C string NULL terminator.

    So? So, when you use printf(), it will stop printing when you use %s, as soon as it meets a NULL terminator, as all the functions of <stdio.h> do. BTW, you had forgotten to include that library.

    One solution would be to use two counters, one for every array and increment the counter of the array that we just filled in.

    Moreover, use >= instead of > to take into account 'a' too. Likewise for 'z', and their capitals.

    Putting them all together you get something like this:

    #include <string.h>
    #include <time.h>
    #include <math.h>
    #include <stdio.h> // you hadn't include that!
    
    #define MAX_LEN 50
    
    int main()
    {
        char str[MAX_LEN] = { 0 };
        char upper[MAX_LEN] = { 0 };
        char lower[MAX_LEN] = { 0 };
        int i = 0;
        int j = 0; // counter for 'upper'
        int k = 0; // counter for 'lower'
    
        printf("Enter a string: ");
        fgets(str, MAX_LEN, stdin);
        str[strcspn(str, "\n")] = 0;
    
        for (i = 0; i < strlen(str); i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z') // use the equal operator as well for reading 'A' and 'Z' as well
            {
                upper[j++] = str[i]; // increment the counter 'j'
            }
            else if (str[i] >= 'a' && str[i] <= 'z') // use the equal operator as well for reading 'a' and 'z' as well
            {
                lower[k++] = str[i]; // increment the counter 'k'
            }
        }
    
        // print your strings, but use a newline for aesthetics
        printf("%s\n", upper);
        printf("%s\n", lower);
    
        return 0;
    }
    

    Output:

    Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
    Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
    Enter a string: Samaras
    S
    amaras