View Full Version : c++ question
wo1olf
02-22-2007, 09:09 PM
how can convert a string type (in capital letters) to small letters .
Ex. From 'A' to 'a' ?
MTsoul
02-22-2007, 11:45 PM
char capital = 'A';
char small = capital + 26;
wo1olf
02-23-2007, 12: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
02-23-2007, 01: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:
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.