?
Solved

Counting Characters, change to upper case

Posted on 2003-03-10
14
Medium Priority
?
242 Views
Last Modified: 2010-04-15
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;
}


0
Comment
Question by:adinkins
  • 9
  • 4
14 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8106627
subtract 32...

else if(c>='a'&&c<='z')
(
  nrLowercase++;
  c -= 32;
)
0
 
LVL 6

Expert Comment

by:gj62
ID: 8106638
But you aren't outputting c, so I'm not sure what you are getting at...

What do you want to uppercase?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8106682
But you aren't outputting c, so I'm not sure what you are getting at...

What do you want to uppercase?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8106690
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);
0
 
LVL 6

Expert Comment

by:gj62
ID: 8106693
add

return 0;
}

to the above.  Sorry 'bout that...

If this is not what you want, please provide more details...
0
 

Author Comment

by:adinkins
ID: 8106759
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
0
 

Author Comment

by:adinkins
ID: 8106816
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
0
 
LVL 6

Accepted Solution

by:
gj62 earned 80 total points
ID: 8106931
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;
}
0
 

Author Comment

by:adinkins
ID: 8106955
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;
}





0
 
LVL 6

Expert Comment

by:gj62
ID: 8106995
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;
}
0
 

Author Comment

by:adinkins
ID: 8107001
Perfect!
Thanks a million.

ald
0
 
LVL 6

Expert Comment

by:gj62
ID: 8107023
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;
}
0
 
LVL 6

Expert Comment

by:gj62
ID: 8107030
Thanks...

Sorry for all the reposts- my browser's been timing out with a blank screen...
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8108163
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");
}


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question