By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,704 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,704 IT Pros & Developers. It's quick & easy.

help with upper and lower case conversion

P: n/a
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';

}
Dec 17 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"B Williams" <wi*******@hotmail.comwrote:
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?
Dec 18 '06 #2

P: n/a

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
"B Williams" <wi*******@hotmail.comwrote:
>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?
If I enter BackDrop, and want the program to produce the output PORdKCAb
Dec 18 '06 #3

P: n/a
In article <Ag*******************@newsfe18.lga>,
"B Williams" <wi*******@hotmail.comwrote:
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
"B Williams" <wi*******@hotmail.comwrote:
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?

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 );
Dec 18 '06 #4

P: n/a

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
In article <Ag*******************@newsfe18.lga>,
"B Williams" <wi*******@hotmail.comwrote:
>"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.ear thlink.net...
"B Williams" <wi*******@hotmail.comwrote:

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?

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 );
Daniel,
where would that go in my code?
Dec 18 '06 #5

P: n/a
On Sun, 17 Dec 2006 20:56:22 -0500 in comp.lang.c++, "B Williams"
<wi*******@hotmail.comwrote,
>transform( name.begin(), name.end(), name.begin(), &swapCase );

Daniel,
where would that go in my code?
You didn't really write any of it, did you?

Dec 18 '06 #6

P: n/a
"B Williams" <wi*******@hotmail.comwrote:
"Daniel T." <da******@earthlink.netwrote:
>"B Williams" <wi*******@hotmail.comwrote:
>>"Daniel T." <da******@earthlink.netwrote:
"B Williams" <wi*******@hotmail.comwrote:

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?

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 );

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 );
}
Dec 18 '06 #7

P: n/a

"David Harmon" <so****@netcom.comwrote in message
news:45****************@news.west.earthlink.net...
On Sun, 17 Dec 2006 20:56:22 -0500 in comp.lang.c++, "B Williams"
<wi*******@hotmail.comwrote,
>>transform( name.begin(), name.end(), name.begin(), &swapCase );

Daniel,
where would that go in my code?

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.
Dec 18 '06 #8

P: n/a

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
"B Williams" <wi*******@hotmail.comwrote:
>"Daniel T." <da******@earthlink.netwrote:
>>"B Williams" <wi*******@hotmail.comwrote:
"Daniel T." <da******@earthlink.netwrote:
"B Williams" <wi*******@hotmail.comwrote:
>
>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?

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 );

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 );
}
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.
Dec 18 '06 #9

P: n/a
In article <Mg***********@newsfe18.lga>,
B Williams <wi*******@hotmail.comwrote:
>
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.ear thlink.net...
>"B Williams" <wi*******@hotmail.comwrote:
>>"Daniel T." <da******@earthlink.netwrote:
"B Williams" <wi*******@hotmail.comwrote:
"Daniel T." <da******@earthlink.netwrote:
>"B Williams" <wi*******@hotmail.comwrote:
>>
>>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?
>>>
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;
}
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

Dec 19 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.