help with upper and lower case conversion

B

B Williams

I have written some code that will take in a string and print out the
reverse, but I also want it to check for upper and lower case and swap them.
Will someone assist me?

include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <algorithm>
using std::reverse;

int main() {
cout << "Enter a string.\n";
string name;
getline(cin, name);
reverse(name.begin(), name.end());
cout << name << '\n';

}
 
D

Daniel T.

B Williams said:
I have written some code that will take in a string and print out the
reverse, but I also want it to check for upper and lower case and swap them.
Will someone assist me?

include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <algorithm>
using std::reverse;

int main() {
cout << "Enter a string.\n";
string name;
getline(cin, name);
reverse(name.begin(), name.end());
cout << name << '\n';

}

I'm not quite sure what you mean, could you provide some sample input
and output? For example should: AbCd turn into dCbA or DcBa?
 
B

B Williams

Daniel T. said:
I'm not quite sure what you mean, could you provide some sample input
and output? For example should: AbCd turn into dCbA or DcBa?

If I enter BackDrop, and want the program to produce the output PORdKCAb
 
D

Daniel T.

"B Williams said:
If I enter BackDrop, and want the program to produce the output PORdKCAb

char swapCase( char c )
{
char result = tolower( c );
if ( islower( c ) )
result = toupper( c );
return result;
}

transform( name.begin(), name.end(), name.begin(), &swapCase );
 
B

B Williams

Daniel T. said:
char swapCase( char c )
{
char result = tolower( c );
if ( islower( c ) )
result = toupper( c );
return result;
}

transform( name.begin(), name.end(), name.begin(), &swapCase );

Daniel,
where would that go in my code?
 
D

David Harmon

On Sun, 17 Dec 2006 20:56:22 -0500 in comp.lang.c++, "B Williams"
Daniel,
where would that go in my code?

You didn't really write any of it, did you?
 
D

Daniel T.

B Williams said:
where would that go in my code?

I have to leave something for you to do. :) The swapCase function can
go anywhere outside of the main function (though if you put it after
'main' or in a different cpp file, you will need to declare it.) The
'transform' line needs to go somewhere between the 'getline' and 'cout
<< name'.

BTW, like 'reverse', 'transform' is a standard algorithm. Here is
another way to write swapCase:

char swapCase( char c ) {
return islower( c ) ? toupper( c ) : tolower( c );
}
 
B

B Williams

David Harmon said:
On Sun, 17 Dec 2006 20:56:22 -0500 in comp.lang.c++, "B Williams"


You didn't really write any of it, did you?
The reverse function is in the C++ standard library, so I didn't have to
write it, but I did know enough to get the syntax correct.
 
B

B Williams

Daniel T. said:
I have to leave something for you to do. :) The swapCase function can
go anywhere outside of the main function (though if you put it after
'main' or in a different cpp file, you will need to declare it.) The
'transform' line needs to go somewhere between the 'getline' and 'cout
<< name'.

BTW, like 'reverse', 'transform' is a standard algorithm. Here is
another way to write swapCase:

char swapCase( char c ) {
return islower( c ) ? toupper( c ) : tolower( c );
}

Thanks Daniel, I got it to work. I have gotten so much from this NG over the
past year that I was attempting to help someone else. I'm no where the help
person yet.
 
Y

Yannick Tremblay

Just a note to the OP:

Please be aware that the above or even the concept of "tolower" is
mainly only valid for basic US-ASCII in basic English.

If you eventually get a job in the software industry, please do not
ever make such a simplistic assumption. Billions of lines of code
have had to be rewritten because of assumption that the software would
never need to move outside the 7 bits ASCII character set.

If that's a course assignment, add a disclaimer to your submission.
If I were the teacher, I would add bonus points to whoever points this
out.

Yan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top