943,852 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1272
  • C++ RSS
Nov 13th, 2008
0

new to c++ __coverting uppercase to lowercase vice versa

Expand Post »
Write a program that will prompt the user to type in a series of characters. Input these characters into a c-string (char array). For each character, if it is a letter, toggle the case (upper to lower case, or lower to upper case). All other characters leave as is. Print out the string before and after.

and

I i've been use:

#include <cctype>

and

islower(), isupper(), tolower(), and toupper(), and maybe isalpha(). to the help the coding.



In the for loop, use this format:
for (i=0; arrayname[i]; i++)

any suggestion?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1s_0n3_guy is offline Offline
6 posts
since Nov 2008
Nov 13th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

What have you done so far? We don't solve homeworks
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

Yes, you've collected all the tools you'll need. Now put them together in some actual code.

I kind of like your for loop - clever way you end it. Goes against my feeling that for loops should be controlled by the value of the counter, but it's more succinct than the equivalent while loop implementation.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 13th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

Click to Expand / Collapse  Quote originally posted by vmanes ...
I kind of like your for loop - clever way you end it.
As do I. Which automatically makes me believe that it's part of the assignment given by the teacher
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

Click to Expand / Collapse  Quote originally posted by niek_e ...
As do I. Which automatically makes me believe that it's part of the assignment given by the teacher

okay i think i got it all together ..check this out..
and it is an assignment.... i was just asking for any input on it...
but here is my code and for the next guy who wants to use it .. i don't mind... in the end its all up to you weather you learn it or not...you might learn something new from it... do what ever you want with it.....


#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

int main ()
{
char letters[20];
char temp;
int i;
cout << "Pease enter characters " << endl;
cin >> letters;
for (i=0; letters[i]; i++)
{
if (isalpha(letters[i]))

{if (islower(letters[i]))
temp=toupper(letters[i]);
if (isupper(letters[i]))
temp=tolower(letters[i]);
cout << temp;
}
else
{temp=letters[i];
cout << temp;
}
}
cout << endl;
cin >> i;

return 0;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1s_0n3_guy is offline Offline
6 posts
since Nov 2008
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

and how could i make it better? fancier or what ever.. i like to get a good grasp on this...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1s_0n3_guy is offline Offline
6 posts
since Nov 2008
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

Here's code from something I was doing the other-night:
C++ Syntax (Toggle Plain Text)
  1. string str = "I'm a fairy! abc xyz ABC XYZ";
  2. int casechanged = 0;
  3.  
  4. cout << str;
  5. for(i = 0; i < str.length(); i++)
  6. {
  7. if(isUpper(str[i])) { str[i] = toLower(str[i]); casechanged = 1; }
  8. if(isLower(str[i]) && casechanged != 1) { str[i] = toUpper(str[i]);}
  9.  
  10. casechanged = 0;
  11. }
  12. cout << str;
  13.  
  14. for(i = 0; i < str.length(); i++)
  15. {
  16. if(isLower(str[i])) { str[i] = toUpper(str[i]); casechanged = 1; }
  17. if(isUpper(str[i]) && casechanged != 1) { str[i] = toLower(str[i]); }
  18.  
  19. casechanged = 0;
  20. }
  21. cout << str;
The isLower/Upper(), and toUpper/toLower(), are my own functions I wrote, just replace them with the proper names of the ones you're using.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

koool thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1s_0n3_guy is offline Offline
6 posts
since Nov 2008
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

Here's your code, cleaned up and simplified a bit.

When posting code here, please use the tags
[code]
your code here
[/code]
This will better preserve your indenting, making the code easier to read.

You should adopt a consistent style of indenting, and use of curly braces. You're mixing styles, which hinders readability. Also use some blank spaces in a line between operators and operands. You're not charged by the byte.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. char letters[20];
  9. int i;
  10.  
  11. cout << "Pease enter characters " << endl;
  12. cin >> letters;
  13.  
  14. cout << letters << endl; //assignment said display the original
  15.  
  16. for ( i = 0; letters[i]; i++ )
  17. {
  18. if ( isalpha(letters[i] ) )
  19. {
  20. if ( islower(letters[i] ) )
  21. letters[i] = (char) toupper( letters[i] );
  22. else //we know it's a letter, and not lowercase
  23. letters[i] = (char) tolower( letters[i] );
  24. }
  25. //don't do anything to non-alpha
  26. }
  27.  
  28. //display the whole string, not one char at a time in the loop
  29. cout << letters << endl; //the changed version
  30.  
  31. cout << endl;
  32.  
  33. return 0;
  34. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 14th, 2008
0

Re: new to c++ __coverting uppercase to lowercase vice versa

thank you ..it does look a lot better ....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1s_0n3_guy is offline Offline
6 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Getting a weird error
Next Thread in C++ Forum Timeline: help in c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC