944,202 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 99226
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 3rd, 2004
0

convert lower case letters to uppercase and vice-versa

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pratima is offline Offline
7 posts
since Sep 2004
Oct 3rd, 2004
0

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

C++ Syntax (Toggle Plain Text)
  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.
ZuK
Reputation Points: 11
Solved Threads: 0
Light Poster
ZuK is offline Offline
29 posts
since Oct 2004
Oct 3rd, 2004
1

Re: help

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 24th, 2007
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehAspirer is offline Offline
4 posts
since Nov 2007
Nov 24th, 2007
0

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

In c++
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 24th, 2007
0

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

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 24th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehAspirer is offline Offline
4 posts
since Nov 2007
Nov 24th, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 24th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehAspirer is offline Offline
4 posts
since Nov 2007
Nov 24th, 2007
0

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

>And to think I thought my knowledge of sappy romantic anime was unsurpassed...
I'm the queen of anime.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Converting binary code to decimal value.
Next Thread in C++ Forum Timeline: Larger Integer Multiplication





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


Follow us on Twitter


© 2011 DaniWeb® LLC