PDA

View Full Version : c++ question



wo1olf
February 22nd, 2007, 10:09 PM
how can convert a string type (in capital letters) to small letters .
Ex. From 'A' to 'a' ?

MTsoul
February 23rd, 2007, 12:45 AM
char capital = 'A';

char small = capital + 26;

wo1olf
February 23rd, 2007, 01:48 PM
thnks. But the thing is i want to do that with a string type, not char :cross-eye
(my teacher doesn't want us to use char type but only string...)

Ben H
February 23rd, 2007, 02:30 PM
To loop through a character array and use tolower is ugly, although probably all you are expected to do. I personally would use the transform function with tolower:



#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string lc_string = "SOME UPPER SoMe LoWeR";
int (*down_func)(int) = tolower;
transform(lc_string.begin(), lc_string.end(), lc_string.begin(), down_func);
cout<<lc_string;
return 0;
}


Hope that helps a bit :thumb:

-Ben

EDIT: Whoops, did lower to upper not upper to lower... Changed it now :trout: