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

C++ string to lowercase method?

P: 12
hello, what i'm wanting to do is take a month input(january february, etc) turn it to a 3 char string(jan, feb, mar) which i have, and then turn them to all lowercase, so as i don't haev to have 8 if statements(Jan, JAn, JaN, JAN, jAN, jaN, jAn, jan) to set up(therefore makeing 84 extra ifstatements, 12 months)

I've tried all sorts of variations, to no avail.(this is my first class in c++, 3rd in comp programing, so very new)

so please make the explanation fairly dumby proof if you can, if not any help is greatly appreciated. thank you.

the variable name is package

string month;
...(extra variables and shit)
getline(cin, month);
...(extra input from user)
month = month.substr(0, 3);

or, another solution to this would be to use something like java's string.equalsIgnoreCase()

ie: if(month.equalsIgnoreCase("jan"))
{
...
}
Nov 8 '06 #1
Share this Question
Share on Google+
15 Replies


P: 12
here's the problem, as i've said before, i'm new to c++, in my opinion its 10 times worse than java, its so much harder to do anything, like int his case lowercase methods, ignorecase methods, many other comparison methods and such, but heres my point, this is a quote from another discussion.

Vadim wrote:[color=blue]
> Hi!
>
> I have a STL string, which I need to convert to low case.
> In VC 6 I used:
> string sInputBuffer = _strlwr((char*)sBuffer.c_str());[/color]

Try this maybe (std C++ - don't know about MVC):

std::transform( sBuffer.begin(), sBuffer.end(), sBuffer.begin(),
::tolower );

Cheers,
Andre
what's sBuffer.begin, where do i put in the string that i wan't to modify, its not a char array, its a string that i'm modifying. please, if you offer help, tell me how its supposed to help me, that whole discussion turns into a bunch of ppl arguing like schoolgirls. I need an example code to look at, not a generic one where i'm supposed to know what all that shit means. like string.substr(int, int);
granted, that is an easy one to dicipher, please say for example:

string s1 = "abc de fgh";
s1 = s1.substr(0, 5); //strings start at 0, not 1

thanks for help.
Nov 8 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
Try this:

Expand|Select|Wrap|Line Numbers
  1. #include <cctype>
  2.  
  3. // cctype.h includes the method tolower(int), which will return the
  4. // lowercase number value of the characters (represented by int).
  5. // For example, if 'A' is 36 and 'a' is 65 (Random numbers, do not
  6. // use these!), then tolower('A') will return 65.
  7.  
  8. char ch = static_cast<char>(tolower(otherCh));
Nov 8 '06 #4

P: 12
ok, well this is a string that i'm working with, not individual char's, i don't know how to seperate strings into individual char's, and i tried imputing month.substr(0, 1) into it and it wouldn't let me. is there no way to manipulate strings like you can in java? thanks for the help
Nov 8 '06 #5

sicarie
Expert Mod 2.5K+
P: 4,677
ok, well this is a string that i'm working with, not individual char's, i don't know how to seperate strings into individual char's, and i tried imputing month.substr(0, 1) into it and it wouldn't let me. is there no way to manipulate strings like you can in java? thanks for the help
String manipulation in C/C++ is more basic. Strings are defined as arrays of characters, so logically, it is arrayOfChars[0-> ( 1 + #of_letters_in_string)] because strings are terminated by having a '\0' character at the end of the array. A string can be identified by the memory location (pointer) to the first element in the array (arrayOfChars[0]). The compiler will take that first character, and continue until it reaches the '\0' element.

This should give you a good overview of strings

http://yolinux.com/TUTORIALS/LinuxTu...ringClass.html
Nov 8 '06 #6

P: 77
ganon if i have a string "In" how would i apply that code then? after cin>> In;
Jun 25 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
hello, what i'm wanting to do is take a month input(january february, etc) turn it to a 3 char string(jan, feb, mar) which i have, and then turn them to all lowercase, so as i don't haev to have 8 if statements(Jan, JAn, JaN, JAN, jAN, jaN, jAn, jan) to set up(therefore makeing 84 extra ifstatements, 12 months)
This is the original problem.

A string object has individual characters. If the object name is str, then the first character is str[0]. the second is str[1], etc. You call the size() method to get the number of characters in the string.

Next, conversion to lowercase can use a function called tolower. It converts uppercase to lower case and does nothing to characters that aren't uppercase.

So, write a loop:
Expand|Select|Wrap|Line Numbers
  1. string str("JAN");
  2. int length = str.size();
  3. for (int i = 0; i < length; ++i)
  4. {
  5.     str[i] = tolower(str[i]);
  6. }
  7.  
There are other more C++-like ways to do this but this is OK for starters.
Jun 25 '07 #8

P: 77
err... the problem now is that the type of input i have is a char [999], so i can't run alot of common solutions... because i cannot convert const char to char? some error like that. !!!
Jul 26 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
Where does this occur?
so i can't run alot of common solutions... because i cannot convert const char to char? some error like that. !!!
Jul 27 '07 #10

P: 77
i don't have permission to post the code so i can post parts...

char In[999];
CString in;

cin >> In;
in = In;

then in is sent through the dictionary. it gets it from the input In.... (in does)

so... if i do in = "love", it will always search love... therefore in depends on In, for what to input.

i never used "cstring" before, so maybe there is a way i can simply check the last letter or each input as it comes from In, for ! or commas and if found, remove them... if not found in = In?

related thread:
http://www.thescripts.com/forum/thread683826.html


update: i tried this, i get some errors that are similar from different functions i tried on my own.


length = in.size();

for (int j = 0; i < length; ++j)
{
in[j] = tolower(in[j]);

}
error:
Translator.cpp(142) : error C2106: '=' : left operand must be l-value
Jul 28 '07 #11

weaknessforcats
Expert Mod 5K+
P: 9,197
Actually, CString is not a C++ string. It is a Microsoft string class that escaped from MFC and found it's way in various pieces of code.

The C++ string class is basic_string. This is a template, which if specialized with a char becomes basic_string<char>. This has been typedef'd to string.
Now you can use string as a type.

By changing your CString to a string, your code compiles.
Jul 30 '07 #12

P: 77
since in is cstring... i had to store another cstring as a temp

so i have cstring temp

so

temp = In // In is my real input complex [999]

in = temp.MakeLower();

then it works! check out my other threads if you need help with cstrings and removing punctuation
Jul 31 '07 #13

weaknessforcats
Expert Mod 5K+
P: 9,197
This:

in = temp.MakeLower();
is not the same as this:
in[j] = tolower(in[j]);
In the first case you are using CString::operator=(). In the second case you are using the C++ operator= that assigns one char to another.

My guess is that CString::operator[] returns a const char& making it impossible to assign to it.

Just check your CString class.
Jul 31 '07 #14

P: 77
oh, umm... well this works now hehe. so thats good, the last post is what i had to do to make it work...

but your probably right too..

i just need to find a way to not crash now if the user types ....................

if there is more then one symbol in a row of the same type it crashes, which bewilders me. i posted my code so please mods dont get mad if i post it again, so i was thinking maybe i should make this a case statement?

//punctuation removal
//while loop checks several times to catch all punctuation

while (punk < 10){
len = strlen(in);
if (in.Left(1) == '"')
in = in.TrimLeft('"');

if (in[len-1] == '.')
in = in.TrimRight('.');

if (in[len-1] == '"')
in = in.TrimRight('"');

if (in[len-1] == '!')
in = in.TrimRight('!');

if (in[len-1] == ',')
in = in.TrimRight(',');

if (in[len-1] == '?')
in = in.TrimRight('?');

if (in[len-1] == ':')
in = in.TrimRight(':');

if (in[len-1] == ';')
in = in.TrimRight(';');

punk++;
}
punk = 0;
//end of punctuation check
Jul 31 '07 #15

P: 77
i think i posted this at 5am or after waking up lol i thought it was my original post, my bad!
Jul 31 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.