| | |
new to c++ __coverting uppercase to lowercase vice versa
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
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?
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?
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.
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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;
}
Here's code from something I was doing the other-night:
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.
C++ Syntax (Toggle Plain Text)
string str = "I'm a fairy! abc xyz ABC XYZ"; int casechanged = 0; cout << str; for(i = 0; i < str.length(); i++) { if(isUpper(str[i])) { str[i] = toLower(str[i]); casechanged = 1; } if(isLower(str[i]) && casechanged != 1) { str[i] = toUpper(str[i]);} casechanged = 0; } cout << str; for(i = 0; i < str.length(); i++) { if(isLower(str[i])) { str[i] = toUpper(str[i]); casechanged = 1; } if(isUpper(str[i]) && casechanged != 1) { str[i] = toLower(str[i]); } casechanged = 0; } cout << str;
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
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.
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)
#include <iostream> #include <cctype> using namespace std; int main () { char letters[20]; int i; cout << "Pease enter characters " << endl; cin >> letters; cout << letters << endl; //assignment said display the original for ( i = 0; letters[i]; i++ ) { if ( isalpha(letters[i] ) ) { if ( islower(letters[i] ) ) letters[i] = (char) toupper( letters[i] ); else //we know it's a letter, and not lowercase letters[i] = (char) tolower( letters[i] ); } //don't do anything to non-alpha } //display the whole string, not one char at a time in the loop cout << letters << endl; //the changed version cout << endl; return 0; }
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Getting a weird error
- Next Thread: help in c++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






