944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 881
  • C++ RSS
Nov 11th, 2009
0

uppercase and lowercase

Expand Post »
Hey...

I've written a program that changes a single character from uppercase to lowercase and viceversa but I want to change a whole word from uppercase to lowercase and viceversa

how do I do that?

C++ Syntax (Toggle Plain Text)
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. for (int i=1;i<=10;i++){
  9.  
  10. char ch;
  11.  
  12. cout<<"Enter a character: ";
  13. cin>> ch;
  14.  
  15. if ( isalpha ( ch ) ) {
  16. if ( isupper ( ch ) )
  17. cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
  18. else
  19. cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) )<<endl;
  20. }
  21. else
  22. cout<<"The character is not a letter"<<endl;
  23.  
  24.  
  25. while ( cin.get ( ch ) && ch != '\n' )
  26. ;
  27. }
  28. system("pause");
  29. return 0;
  30. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
alexa868 is offline Offline
19 posts
since Nov 2009
Nov 11th, 2009
0
Re: uppercase and lowercase
Go through the word character by character and change each character to upper or lower case using toupper or tolower from cctype. There is no toupper or tolower function that takes a string as a parameter and converts the whole string.

I'm sure many people have written one, but it's not a standard function.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 12th, 2009
0
Re: uppercase and lowercase
I'm sure many people have written one
Here's mine:

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <cctype>
  5.  
  6. struct upper {
  7. int operator() ( int c )
  8. {
  9. return toupper ( static_cast<unsigned char> ( c ) );
  10. }
  11. };
  12.  
  13. struct lower{
  14. int operator() ( int c )
  15. {
  16. return tolower( static_cast<unsigned char> ( c ) );
  17. }
  18. };
  19.  
  20. int main(){
  21. std::string str = "Test StrinG";
  22. std::transform(str.begin(),str.end(),str.begin(),upper());
  23. std::cout << str;
  24. std::transform(str.begin(),str.end(),str.begin(),lower());
  25. std::cout << str;
  26. return 0;
  27. }
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 12th, 2009
0
Re: uppercase and lowercase
Which flavor of c++ are you using? I use VC++ for work and I use the MakeLower() function on strings.

Also, here's a dreamincode thread about a similar issue.
Reputation Points: 56
Solved Threads: 18
Posting Whiz in Training
donaldw is offline Offline
250 posts
since Nov 2009
Nov 12th, 2009
0
Re: uppercase and lowercase
Click to Expand / Collapse  Quote originally posted by donaldw ...
Which flavor of c++ are you using? I use VC++ for work and I use the MakeLower() function on strings.
I wasn't thinking about flavors. I was thinking about the standard libraries listed here that are available to everyone and are portable:

http://www.cplusplus.com/reference/

I know of no function in this set of functions that will do the job. But you're right. Visual C++ will have a lot of things beyond that. You can also look at the Boost functions. They could be in there too.
Last edited by VernonDozier; Nov 12th, 2009 at 11:29 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Square Problem HELP
Next Thread in C++ Forum Timeline: Opening Web page and control elements of the web.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC