?
Solved

Counting Characters, change to upper case

Posted on 2003-03-10
14
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

801 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