954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Counting The upper case

Here is what i got. I need to count the Uppercase entered in a string. Can someone help me?

Enter string: The Brown Fox
#of Upper Case: 3


My Progress:

#include
#include
usingnamespace std;
int main()
{
char string[15];
cout<<"Enter string: ";
cin.getline(string,15);
.
.
.
.
}
I need the next solution thanks!

jman2011
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Here is a hint: it involves #include <cctype> (or #include <locale> ) and std::isupper .

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 
Here is a hint: it involves #include <cctype> (or #include <locale> ) and std::isupper .

No condition

if(isupper(c)) something

jman2011
Newbie Poster
5 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
int j=0;
for(int i=0;i<26;++i)
if(isupper(string[i])==1)
j+=1;
adityatandon
Junior Poster
114 posts since Dec 2011
Reputation Points: 33
Solved Threads: 12
 

try this it works

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *str;
public:
      void input()
      {
       cout<<"\nenter the string"<<endl;
       cin>>str;
       }
      void display()
      {
       cout<<"\n"<<str;
      }
      int friend upper(string);
};
int upper(string a)
{
int j=0;

while((*a.str)!='\0')
  {
   if((*a.str)>=65&&(*a.str)<91)
   {
   j++;
   (a.str)++;
   }
   else
   (a.str)++;
   }
  return(j);
 }



int main()
{
clrscr();
int n;
 string b;
 b.input();
 b.display();
n=upper(b);
cout<<"no of upper case letters are::"<<n;
getch();
return(0);
}

:)

sukhi badhe
Newbie Poster
23 posts since Jan 2012
Reputation Points: 6
Solved Threads: 1
 

Yeah... but just using the code i gave above is a substitute for all of it..
Isupper resides in the standard library with the same procedure u mentioned here.. Why write the whole code, when there already exists a direct function for it ??

adityatandon
Junior Poster
114 posts since Dec 2011
Reputation Points: 33
Solved Threads: 12
 

Since we appear to be giving away code these days, you can do this in a single line using std::count_if :

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    /* Read the target sentence into a std::string */
    std::string sentence;
    std::cout << "Enter a sentence:" << std::endl;
    getline( std::cin, sentence );

    /* Here's the actual call that counts the number of upper case letters */
    size_t iUpperCount = std::count_if( sentence.begin(), sentence.end(), isupper );

    /* Output the result */
    std::cout << "There are " << iUpperCount << " uppercase letters" << std::endl;

    return 0;
}

You're potentially not expected to know about the standard algorithms in whatever course this task is for, but it's always good to know that they're there :)

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

@aditya u are right my friend

sukhi badhe
Newbie Poster
23 posts since Jan 2012
Reputation Points: 6
Solved Threads: 1
 
Yeah... but just using the code i gave above is a substitute for all of it.. Isupper resides in the standard library with the same procedure u mentioned here.. Why write the whole code, when there already exists a direct function for it ??


Yeah... and by just using the built in functions, what do you learn? How to use a built in function. What if your next language doesn't have that built in function? You can't program the task...

AndSince we appear to be giving away code these days
thread closed...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You