convert lower case letters to uppercase and vice-versa

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

Join Date: Sep 2004
Posts: 7
Reputation: pratima is an unknown quantity at this point 
Solved Threads: 0
pratima pratima is offline Offline
Newbie Poster

convert lower case letters to uppercase and vice-versa

 
0
  #1
Oct 3rd, 2004
i must write a program that will convert lower case letters to upper case and vice versa and if a number is entered it must display char is not a letter
here is what ave written but when i run it it does not do the conversion.thanks
//to convert lower case letters to uppercase and vice-versa
  1.  
  2. #include<iostream.h>
  3. #include<conio.h>
  4.  
  5. void main()
  6. {
  7. clrscr();
  8. char ch;
  9.  
  10. cout<<"enter a character";
  11.  
  12. cin>>ch;
  13. {
  14. if((ch>=65)&&(ch<=122))
  15. {
  16. if((ch>=97)&&(ch<=122))
  17. ch=ch-32;
  18.  
  19. cout<<"The upper case character corresponding is"<<endl;}
  20.  
  21. {if((ch>=65)&&(ch<=90))
  22. ch=ch+32;
  23.  
  24. cout<<"The lower case character corresponding is"<<endl;}
  25.  
  26.  
  27. cout<<"the character is not a letter"<<endl;
  28. }
  29. getch();
  30. }
Last edited by Ancient Dragon; Nov 24th, 2007 at 8:39 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: convert lower case letters to uppercase and vice-versa

 
0
  #2
Oct 3rd, 2004
  1. cout<<"The upper case character corresponding is"<<endl;
you are not outputting ch.

besides this there are some "else" missing.
and you should not use the old headers
K.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help

 
1
  #3
Oct 3rd, 2004
>but when i run it it does not do the conversion
You don't print the value of ch after the conversion.

>#include<iostream.h>
This is an old header that is no longer a part of the C++ language. Use <iostream> instead. You also need to consider namespaces, but for now simply adding using namespace std; will suffice.

>#include<conio.h>
This is a nonstandard header that is not supported by all compilers. Even those compilers that do support it offer different contents. That's definitely not conducive to robust and portable programs. Remove this and replace getch with cin.get.

>void main()
main does not, and never has, had a return type of void. The return type of main is int, and anything else is wrong.

>clrscr();
This is one of the functions that is pretty much only supported by Borland. Clearing the screeen when running a console program is almost always antisocial and it serves no useful purpose, so you shouldn't do it.

>{
There's no need to introduce a block before your first if statement. You can safely remove it because it's just clutter.

>if((ch>=65)&&(ch<=122))
Don't use the ASCII values for a test like this, they're not portable at all. A slightly better way would be:
  1. if ( ch >= 'A' && ch <= 'z' )
But there are two distinct issues with this. The first is that between the upper case letters and the lower case letters in the ASCII table, there are special characters starting at 91 and ending at 96 that are not letters. You handle this later, sort of, but a single test is more clear:
  1. if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) )

The second issue is that while you are no longer using ASCII codes in favor of something more portable, you would still be relying on the fact that the letters have contiguous values. This is not true of all character sets, and ASCII is not the only character set out there. While you aren't likely to be using a machine that has something besides ASCII, it's a good idea to get into the habit of programming portably as early as possible.

So what's the easy solution for the second issue? Use the standard library function isalpha in the <cctype> header:
  1. if ( isalpha ( ch ) )
This covers both the first and second issues, but some teachers will freak if you use something that hasn't been taught even if it is far superior to the naive way they were teaching.

>if((ch>=97)&&(ch<=122))
This is where you make the attempt at determining lower case, but the same arguments apply as to the test two lines up. A much better solution would be the standard function islower, also in the <cctype> header.

>ch=ch-32;
Once again, using ASCII code is not portable or obvious. You can do this instead if you don't mind assuming the ASCII character set (because it relies on the letters being contiguous):
  1. ch += 'A' - 'a';
It's a more idiomatic way of saying that you want to convert an ASCII lower case letter to upper case. 'A' - 'a' is -32 in ASCII, so by adding that to ch, you subtract 32 and have the upper case equivalent. The inverse can be done with a similar test:
  1. ch += 'a' - 'A';
Of course, for both tests and even better solution is toupper and tolower from <cctype>. That way your code is portable.

Here is the code with all of the changes I've suggested:
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char ch;
  9.  
  10. cout<<"Enter a character: ";
  11. cin>> ch;
  12.  
  13. if ( isalpha ( ch ) ) {
  14. if ( isupper ( ch ) ) {
  15. ch = tolower ( ch );
  16.  
  17. cout<<"The lower case equivalent is "<< ch <<endl;
  18. }
  19. else {
  20. ch = toupper ( ch );
  21.  
  22. cout<<"The upper case equivalent is "<< ch <<endl;
  23. }
  24. }
  25. else
  26. cout<<"The character is not a letter"<<endl;
  27.  
  28. cin.get();
  29. }
There is still a problem though. The first call to cin>> probably left a newline in the input stream. Because cin.get stops reading at a newline, it will appear that the call to cin.get is ignored. The solution is to read characters until a newline is reached. You can do it any number of ways, but the simplest is a loop:
  1. while ( cin.get ( ch ) && ch != '\n' )
  2. ;
Also, we can tighten up the code by removing the assignment to ch when performing the case conversion. We can do this because the new value of ch isn't used except for printing once:
  1. #include <cctype>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char ch;
  9.  
  10. cout<<"Enter a character: ";
  11. cin>> ch;
  12.  
  13. if ( isalpha ( ch ) ) {
  14. if ( isupper ( ch ) )
  15. cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
  16. else
  17. cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) ) <<endl;
  18. }
  19. else
  20. cout<<"The character is not a letter"<<endl;
  21.  
  22. // Flush the input stream
  23. while ( cin.get ( ch ) && ch != '\n' )
  24. ;
  25.  
  26. cin.get();
  27. }
I'm sure you'll notice that I used static_cast<char> for the toupper and tolower calls. The reason for this is because cout automagically determines the type of the object and prints it accordingly. Because toupper and tolower return int, cout will print the ASCII codes rather than the character representations. So the values must be cast to char before they will be printed correctly. This was done implicitly when we were assigning the result of toupper and tolower to ch, but good compilers would give you a warning about assigning from a wider type (int) to a narrower type (char). For the first code to compile cleanly, a cast would still have been needed.

Here is the same code assuming ASCII, without using the useful library functions that your teacher will probably bitch at you for using.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char ch;
  8.  
  9. cout<<"Enter a character: ";
  10. cin>> ch;
  11.  
  12. if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) ) {
  13. if ( ch >= 'A' && ch <= 'Z' )
  14. cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  15. else
  16. cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  17. }
  18. else
  19. cout<<"The character is not a letter"<<endl;
  20.  
  21. // Flush the input stream
  22. while ( cin.get ( ch ) && ch != '\n' )
  23. ;
  24.  
  25. cin.get();
  26. }
Another way to order the tests to make things slightly clearer would be:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char ch;
  8.  
  9. cout<<"Enter a character: ";
  10. cin>> ch;
  11.  
  12. if ( ch >= 'A' && ch <= 'Z' )
  13. cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  14. else if ( ch >= 'a' && ch <= 'z' )
  15. cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  16. else
  17. cout<<"The character is not a letter"<<endl;
  18.  
  19. // Flush the input stream
  20. while ( cin.get ( ch ) && ch != '\n' )
  21. ;
  22.  
  23. cin.get();
  24. }
If you aren't allowed to use the functions from <cctype> and you still need something portable then you could write your own. They wouldn't be nearly as efficient though because library functions themselves use nonportable constructs and hide them in a standardized interface that works everywhere. The easiest way to write portable isupper, islower, toupper, and tolower functions would be by searching for the character in a string:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool is_upper ( char ch )
  6. {
  7. static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8.  
  9. for ( int i = 0; upper[i] != '\0'; i++ ) {
  10. if ( ch == upper[i] )
  11. return true;
  12. }
  13.  
  14. return false;
  15. }
  16.  
  17. bool is_lower ( char ch )
  18. {
  19. static char lower[] = "abcdefghijklmnopqrstuvwxyz";
  20.  
  21. for ( int i = 0; lower[i] != '\0'; i++ ) {
  22. if ( ch == lower[i] )
  23. return true;
  24. }
  25.  
  26. return false;
  27. }
  28.  
  29. char to_lower ( char ch )
  30. {
  31. static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  32. static char lower[] = "abcdefghijklmnopqrstuvwxyz";
  33.  
  34. for ( int i = 0; upper[i] != '\0'; i++ ) {
  35. if ( ch == upper[i] )
  36. return lower[i];
  37. }
  38.  
  39. return ch;
  40. }
  41.  
  42. char to_upper ( char ch )
  43. {
  44. static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  45. static char lower[] = "abcdefghijklmnopqrstuvwxyz";
  46.  
  47. for ( int i = 0; lower[i] != '\0'; i++ ) {
  48. if ( ch == lower[i] )
  49. return upper[i];
  50. }
  51.  
  52. return ch;
  53. }
  54.  
  55. int main()
  56. {
  57. char ch;
  58.  
  59. cout<<"Enter a character: ";
  60. cin>> ch;
  61.  
  62. if ( is_upper ( ch ) )
  63. cout<<"The lower case equivalent is "<< to_lower ( ch ) <<endl;
  64. else if ( is_lower ( ch ) )
  65. cout<<"The upper case equivalent is "<< to_upper ( ch ) <<endl;
  66. else
  67. cout<<"The character is not a letter"<<endl;
  68.  
  69. // Flush the input stream
  70. while ( cin.get ( ch ) && ch != '\n' )
  71. ;
  72.  
  73. cin.get();
  74. }
Of course, that's probably beyond the scope of your homework, but it's an entertaining exercise that kills a little bit of time.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: tehAspirer is an unknown quantity at this point 
Solved Threads: 0
tehAspirer tehAspirer is offline Offline
Newbie Poster

Re: convert lower case letters to uppercase and vice-versa

 
0
  #4
Nov 24th, 2007
NICE reply Naru! Or is it Narue? =P Anyways, I have a similar question. Mine should be easier, though ^^;

I just need to make my whole script output all caps. I could have just written my cout's in all caps, but I think he wants me to have a function do it, roflwaffle. Anyway to do this or did I get roflpwnt?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,334
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1454
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: convert lower case letters to uppercase and vice-versa

 
0
  #5
Nov 24th, 2007
In c++
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string str = "How now brown cow.";
  9. transform(str.begin(),str.end(),str.begin(),toupper);
  10. cout << str;
  11. return 0;
  12. }
Last edited by Ancient Dragon; Nov 24th, 2007 at 8:45 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert lower case letters to uppercase and vice-versa

 
0
  #6
Nov 24th, 2007
>NICE reply Naru! Or is it Narue? =P
Thank you. And it's Narue, as you can clearly see by the fact that I had to type the name when I created this account, and I'm perfectly capable of spelling it correctly.

>roflwaffle. Anyway to do this or did I get roflpwnt?
It's my deepest hope that you're joking. Anyway, since your teacher probably won't accept transform as a solution, have you considered storing the input as a string, looping through the string, and using toupper?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: tehAspirer is an unknown quantity at this point 
Solved Threads: 0
tehAspirer tehAspirer is offline Offline
Newbie Poster

Re: convert lower case letters to uppercase and vice-versa

 
0
  #7
Nov 24th, 2007
Well no, (and it's a stupid question to begin with) but the question asks that all output be made caps. The question has parts a, b, c, e, and f, where f is the part that asks the caps thing. What this means is I have a ton of cout's for parts a through e, and I was just wonderin' if by chance there was function I could put at the beginning of the script that makes every letter a CAP =P I'm totally new to C++, but if I didn't ask stupid questions, I'd never figure out that they were stupid in the first place ;D



P.S. I'm hecka wierd, so sorry about the roflpwnt's and the roflwaffle's, LOL.
Oh! And why I asked if your name was Naru instead of Narue, was 'cause your little anime character looked alot like Narusegawa, a character from an anime who's name is abbreviated by Naru. Hoho.
Last edited by tehAspirer; Nov 24th, 2007 at 8:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert lower case letters to uppercase and vice-versa

 
0
  #8
Nov 24th, 2007
>I was just wonderin' if by chance there was function I could
>put at the beginning of the script that makes every letter a CAP
Sorry, unless you anticipated that issue in the first place and set up the proper framework, you're SOL and have to manually convert all of the string literals to use upper case.

>And why I asked if your name was Naru instead of Narue, was
>'cause your little anime character looked alot like Narusegawa
I get that a lot, seeing as how Love Hina is a well known anime/manga.

>a character from an anime who's name is abbreviated by Naru.
Actually, her full name is Narusegawa Naru, so it's not an abbreviation at all. The given name of the character is actually Naru.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: tehAspirer is an unknown quantity at this point 
Solved Threads: 0
tehAspirer tehAspirer is offline Offline
Newbie Poster

Re: convert lower case letters to uppercase and vice-versa

 
0
  #9
Nov 24th, 2007
Crap! You're right. And to think I thought my knowledge of sappy romantic anime was unsurpassed...

I'll just put my cout's in all caps =P Thanks for the help, hopefully I'll be back with some more lame questions in dah futurezzz!
Last edited by tehAspirer; Nov 24th, 2007 at 9:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert lower case letters to uppercase and vice-versa

 
0
  #10
Nov 24th, 2007
>And to think I thought my knowledge of sappy romantic anime was unsurpassed...
I'm the queen of anime.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC