Link to home
Start Free TrialLog in
Avatar of adinkins
adinkins

asked on

Counting Characters, change to upper case

How do i change the output to uppercase, after I have counted them.
Please help

See code below:

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

int main() {
     int nrBlanks = 0, nrOthers = 0, nrUppercase = 0, nrLowercase = 0, nrTotal = 0;
     int c;
   
  printf ("Please enter text:\n");
  scanf ("%c", &c);
 
  while( (c = getchar()) != '\n' )         /* combined priming and updating parts of loop */

     if (c >= 'A' && c <= 'Z')
          nrUppercase++;
     else if (c >= 'a' && c <= 'z')
          nrLowercase++;
     else if (c == ' ')
          nrBlanks++;
     else
          nrOthers++;

     nrTotal = nrBlanks + nrLowercase + nrUppercase + nrOthers;
 
     printf("\nUpper case letters:\t%d\nLower case letters:\t%d\nBlank spaces\t  :\t%d\nOther characters  :\t%d\nGrand total\t  :\t%d\n\n",
         nrUppercase, nrLowercase, nrBlanks, nrOthers, nrTotal,
         nrUppercase + nrLowercase, nrBlanks, + nrOthers);

 
  return 0;
}


Avatar of gj62
gj62

subtract 32...

else if(c>='a'&&c<='z')
(
  nrLowercase++;
  c -= 32;
)
But you aren't outputting c, so I'm not sure what you are getting at...

What do you want to uppercase?
But you aren't outputting c, so I'm not sure what you are getting at...

What do you want to uppercase?
Try this:

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

int main() {

    int nrBlanks = 0, nrOthers = 0, nrUppercase = 0, nrLowercase = 0, nrTotal = 0;
    int c,q=0;
    char string[81]; /* can't enter more than 80 chars */
 
 printf ("Please enter text:\n");
 
 while( (c = getchar()) != '\n' )         /* combined priming and updating parts of loop */
 {
    if (c >= 'A' && c <= 'Z')
         nrUppercase++;
    else if (c >= 'a' && c <= 'z')
     {
         nrLowercase++;
      c -= 32;
     }
    else if (c == ' ')
         nrBlanks++;
    else
         nrOthers++;

    string[q++] = c;

    nrTotal = nrBlanks + nrLowercase + nrUppercase + nrOthers;

    printf("\nUpper case letters:\t%d\nLower case letters:\t%d\nBlank spaces\t  :\t%d\nOther characters  :\t%d\nGrand total\t  :\t%d\n\n",
        nrUppercase, nrLowercase, nrBlanks, nrOthers, nrTotal,
        nrUppercase + nrLowercase, nrBlanks, + nrOthers);
 }
 string[q] = 0;
 printf("Capitalized string = %s\n", string);
add

return 0;
}

to the above.  Sorry 'bout that...

If this is not what you want, please provide more details...
Avatar of adinkins

ASKER

I want to prompt for input, output all uppercase and then count the initial input, lower, upper, blank spaces, other and total.  Is there antway I can define a const yo print out all uppercase?  The code that you gave, loops the count around 5 times.

ald
My original code gives me part of what I want.  Right after I enter the text, I want the text printed out in uppercase,  I then want to do a count on the original input.

Any help would be appreciated.
ald
ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh sorry 'bout that. my original code did prompt for an input, which is ok, but i did want to prompt for an input:

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

int main() {
    int nrBlanks = 0, nrOthers = 0, nrUppercase = 0, nrLowercase = 0, nrTotal = 0;
    int c;
 
 while( (c = getchar()) != '\n' )         /* combined priming and updating parts of loop */

    if (c >= 'A' && c <= 'Z')
         nrUppercase++;
    else if (c >= 'a' && c <= 'z')
         nrLowercase++;
    else if (c == ' ')
         nrBlanks++;
    else
         nrOthers++;

    nrTotal = nrBlanks + nrLowercase + nrUppercase + nrOthers;

    printf("\nUpper case letters:\t%d\nLower case letters:\t%d\nBlank spaces\t  :\t%d\nOther characters  :\t%d\nGrand total\t  :\t%d\n\n",
        nrUppercase, nrLowercase, nrBlanks, nrOthers, nrTotal,
        nrUppercase + nrLowercase, nrBlanks, + nrOthers);


 return 0;
}





Hmmm, not quite.

In your code, you have

 int c;
 
 printf ("Please enter text:\n");
 scanf ("%c", &c);

This lets you enter a single character - anything else will overflow c.  Not sure how you think this is working if you are entering a string...

Just move the printf outside the while loop, after the printing of the uppercase string:

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

int main() {

   int nrBlanks = 0, nrOthers = 0, nrUppercase = 0, nrLowercase = 0, nrTotal = 0;
   int c,q=0;
   char string[81]; /* can't enter more than 80 chars */
 
printf ("Please enter text:\n");

while( (c = getchar()) != '\n' )         /* combined priming and updating parts of loop */
{
   if (c >= 'A' && c <= 'Z')
        nrUppercase++;
   else if (c >= 'a' && c <= 'z')
    {
        nrLowercase++;
     c -= 32;
    }
   else if (c == ' ')
        nrBlanks++;
   else
        nrOthers++;

   string[q++] = c;

   nrTotal = nrBlanks + nrLowercase + nrUppercase + nrOthers;

}
string[q] = 0;
printf("Capitalized string = %s\n", string);
   printf("\nUpper case letters:\t%d\nLower case letters:\t%d\nBlank spaces\t  :\t%d\nOther characters  :\t%d\nGrand total\t  :\t%d\n\n",
       nrUppercase, nrLowercase, nrBlanks, nrOthers, nrTotal,
       nrUppercase + nrLowercase, nrBlanks, + nrOthers);

return 0;
}
Perfect!
Thanks a million.

ald
Hmmm, not quite.

In your code, you have

 int c;
 
 printf ("Please enter text:\n");
 scanf ("%c", &c);

This lets you enter a single character - anything else will overflow c.  Not sure how you think this is working if you are entering a string...

Just move the printf outside the while loop, after the printing of the uppercase string:

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

int main() {

   int nrBlanks = 0, nrOthers = 0, nrUppercase = 0, nrLowercase = 0, nrTotal = 0;
   int c,q=0;
   char string[81]; /* can't enter more than 80 chars */
 
printf ("Please enter text:\n");

while( (c = getchar()) != '\n' )         /* combined priming and updating parts of loop */
{
   if (c >= 'A' && c <= 'Z')
        nrUppercase++;
   else if (c >= 'a' && c <= 'z')
    {
        nrLowercase++;
     c -= 32;
    }
   else if (c == ' ')
        nrBlanks++;
   else
        nrOthers++;

   string[q++] = c;

   nrTotal = nrBlanks + nrLowercase + nrUppercase + nrOthers;

}
string[q] = 0;
printf("Capitalized string = %s\n", string);
   printf("\nUpper case letters:\t%d\nLower case letters:\t%d\nBlank spaces\t  :\t%d\nOther characters  :\t%d\nGrand total\t  :\t%d\n\n",
       nrUppercase, nrLowercase, nrBlanks, nrOthers, nrTotal,
       nrUppercase + nrLowercase, nrBlanks, + nrOthers);

return 0;
}
Thanks...

Sorry for all the reposts- my browser's been timing out with a blank screen...
even if this is closed, i have an alternate solution and may be 'bit' more efficient,

#define MASK 0x20

char c;

to convert to uppercase
c &=(~MASK);
to convert to lowercase
c |=(MASK);
and its upto you to check that 'c' lies in alphabet ( english )

here is sample

#include <stdio.h>
#define MASK 0x20
main(){
 int c;
for(c='a';c<='z';c++)printf("%c",c&(~MASK));
printf("\n");

for(c='A';c<='Z';c++)printf("%c",c|MASK);
printf("\n");
}