Jack Huang Jack Huang - 4 years ago 96
C Question

C program add individual digits, lower and uppercase letters, and other characters

I'm trying to have my program print these:


  • count each individual digit (0-9)

  • count each individual alphabet letter (a-z and A-Z)

  • total count of any other kind of character



The output is supposed to be the values stored in (10+26+26+1=) 63 counters
My professor hasn't really been helpful as well as my TA as they never really got into this; they just assumed we knew C right off the bat and unresponsive in emails.

#include<stdio.h>
#define MAXLINE 1000
main()
{
char str [MAXLINE];
int ndigit, nlower, nupper, nother;
int c;
printf("Enter any text with numbers or other characters if you like: ");
fgets(str,MAXLINE,stdin);
ndigit = nlower = nupper = nother = 0;

while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
++ndigit;
else if (c >= 'a' && c <= 'a')
++nlower;
else if (c >= 'A' && c <= 'A')
++nupper;
else
++nother;
}
printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other characters %d\n", ndigit, nlower, nupper, nother);
}
}

Answer Source

I tried running your program, There are 2 mistakes in your program.

  1. Whatever strings you type in input, everything is getting stored in str because of line fgets(str,MAXLINE,stdin) so code execution only move forward to the next line and to getchar(), after you press enter or EOF.See This Question What does fgets do? for how fgets work.Now Because getchar never get to work on your input whole while loop become useless.

  2. Above was main problem, removing fgets() will allow getchar() to take characters and loop will work.Another Problem is in line else if (c >= 'a' && c <= 'a') and line else if (c >= 'A' && c <= 'A'), you have mistakenly instead of z and Z made them a and A, so it won't work.

Correcting Both Of These Your Code Will Work Good.

This is your program.

#include<stdio.h>
#define MAXLINE 1000
main()
{
char str [MAXLINE];
int ndigit, nlower, nupper, nother;
char  c;
printf("Enter any text with numbers or other characters if you like: ");
fgets(str,MAXLINE,stdin);     //What Really Happens is whatever you are inputting is getting stored into the char array str
                              //Only after you press enter or eof execution move forward to below.
  //So you type the whole string and then it all get stored in str and then 
  //comes no output because getchar never executes.

ndigit = nlower = nupper = nother = 0;

while ((c = getchar()) != '\n')  
{
        if (c >= '0' && c <= '9')
                ++ndigit;
        else if (c >= 'a'  && c <= 'a') //Change To (c>='a' && c<='z')
                ++nlower;
        else if (c >= 'A' && c <= 'A')  //Change To (c>='A' && c<='Z')
                ++nupper;
        else
                ++nother;

             //   printf("hello world");
}
        printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other             characters %d\n", ndigit, nlower, nupper, nother);
}

After Changing Your Code Becomes.

#include<stdio.h>
#define MAXLINE 1000  //Redundant Line As You Are No Longer Using fgets.
int main()            //Changed to int main()
{

char str [MAXLINE];   //Redundant Line As You Are No Longer Using fgets.
int ndigit, nlower, nupper, nother;
char  c;
printf("Enter any text with numbers or other characters if you like: ");
ndigit = nlower = nupper = nother = 0;

while ((c = getchar()) != '\n')
{
        if (c >= '0' && c <= '9')
                ++ndigit;
        else if (c >= 'a'  && c <= 'z')
                ++nlower;
        else if (c >= 'A' && c <= 'Z')
                ++nupper;
        else
                ++nother;


}
        printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other characters %d\n", ndigit, nlower, nupper, nother);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download