uppercase and lowercase

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 8
Reputation: alexa868 is an unknown quantity at this point 
Solved Threads: 0
alexa868 alexa868 is offline Offline
Newbie Poster

uppercase and lowercase

 
0
  #1
Nov 11th, 2009
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,840
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
Nov 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac
 
0
  #3
Nov 12th, 2009
Originally Posted by VernonDozier View Post
I'm sure many people have written one
Here's mine:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 182
Reputation: donaldw is an unknown quantity at this point 
Solved Threads: 11
donaldw donaldw is offline Offline
Junior Poster
 
0
  #4
Nov 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,840
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #5
Nov 12th, 2009
Originally Posted by donaldw View Post
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.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 400 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC