Mystery1317
Mystery1317

Reputation: 1

how to print an uppercase,lowercase and number in c

I'm in the middle of homework and I need some help here.

So this is the code

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    int upper=0,lower=0,number = 0;
    char ch[500];
    printf("Enter the String:\n");
    gets(ch);
    i=0;
    while(ch[i]!=0)
    {
        if(ch[i]>='A' && ch[i]<='Z')
        {
            upper++;
        }
        else if(ch[i]>='a' && ch[i]<='z')
        {
            lower++;
        }
        else if(ch[i]>='0' && ch[i]<='9')
        {
            number++;
        }
        i++;
    }
printf("lowercase letters: %d",lower);
printf("\nuppercase letters: %d",upper);
printf("\nnumber letters: %d",number);
getch();
    return 0;
}

As you can see here. When you give a string input. The code will give a total number of uppercase,lowercase and number

for example: If I'm giving "Hello World 123" To the code. The result will be 2 Uppercases 8 Lowercases and 3 Numbers

The problem is the task want me to print all 3 types of letter seperately

for example: from "Hello World 123" Should print "HW" , "elloorld" and "123"

I know that I have to create another 3 arrays for seperate the letter. I tried to create like upperc[i],lowerc[i] and num[i] to input the letter in each of if command but It doesn't work.

So how can I do that?

Upvotes: 0

Views: 1908

Answers (3)

TheNisham
TheNisham

Reputation: 13

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

#define MAX_IN 128
#define MAX_X 64

int main()
{
    int i;
    int upper=0,lower=0,number = 0;
    char ch[MAX_IN], chUpper[MAX_X], chLower[MAX_X], chNumber[MAX_X];
    printf("Enter the String:\n");
    gets(ch);
    printf("input given is : %s\n",ch);
    for (i=0;ch[i]!='\0';i++)
    {
        if(ch[i]>='A' && ch[i]<='Z')
        {
            chUpper[upper++]= ch[i];
        }
        else if(ch[i]>='a' && ch[i]<='z')
        {
            chLower[lower++]= ch[i];
        }
        else if(ch[i]>='0' && ch[i]<='9')
        {
            chNumber[number++]= ch[i];
        }
        if ((ch[i+1]) == ' '){
            i += 1;
        }
    }
    printf("lowercase letters: %d: %s\n",lower, chLower);
    printf("uppercase letters: %d: %s\n",upper, chUpper);
    printf("number letters: %d: %s\n",number, chNumber);
    return 0;
}

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

For your simple homework project you must assume some decent values. We assume the input is no longer than 128 characters and each of the three types is no more than 64. In real life projects we must check on these values.

Adapting your main, the following would work:

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

#define MAX_IN 128
#define MAX_X 64

int main()
{
    int i;
    int upper=0,lower=0,number = 0;
    char ch[MAX_IN], chUpper[MAX_X], chLower[MAX_X], chNumber[MAX_X];
    printf("Enter the String:\n");
    gets(ch);
    i=0;
    while(ch[i]!=0)
    {
        if(ch[i]>='A' && ch[i]<='Z')
        {
            chUpper[upper++]= ch[i];
        }
        else if(ch[i]>='a' && ch[i]<='z')
        {
            chLower[lower++]= ch[i];
        }
        else if(ch[i]>='0' && ch[i]<='9')
        {
            chNumber[number++]= i;
        }
        i++;
    }
    chLower[lower]= '\0';
    chUpper[upper]= '\0';
    chNumer[number]= '\0';

    printf("lowercase letters: %d: %s\n",lower, chLower);
    printf("uppercase letters: %d: %s\n",upper, chUpper);
    printf("number letters: %d: %s\n",number, chNumber);
    getch();
    return 0;
}

Upvotes: 0

mediocrevegetable1
mediocrevegetable1

Reputation: 4217

Here's one way of going about it. It doesn't require 3 separate arrays like you thought because it prints the characters directly:

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

int main(void)
{
    printf("Enter a string: ");
    char str[1024] = {0};
    fgets(str, sizeof str, stdin);
    
    // Use different functions in each iteration
    int (*ctype_fns[])(int) = {isupper, islower, isdigit};

    for (unsigned i = 0; i < sizeof ctype_fns / sizeof *ctype_fns; ++i)
    {
        for (const char *it = str; *it; ++it)
        {
            // If char matches requirements, print it
            if (ctype_fns[i]((unsigned char)*it))
                putchar(*it);
        }
        putchar('\n');
    }
}

Example of running:

Enter a string: Hello World 123
HW
elloorld
123

Upvotes: 1

Related Questions