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.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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 :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Here's code from something I was doing the other-night:
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;
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.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
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.
#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;
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228