473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ string to lowercase method?

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
15 48575
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
3,652 Expert 2GB
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
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
4,677 Expert Mod 4TB
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
jerger
77
ganon if i have a string "In" how would i apply that code then? after cin>> In;
Jun 25 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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
jerger
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
9,208 Expert Mod 8TB
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
jerger
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
9,208 Expert Mod 8TB
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
jerger
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
9,208 Expert Mod 8TB
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
jerger
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
jerger
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

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

Similar topics

10
by: Hank Kingwood | last post by:
This is probably an easy question, but I can't find a function (maybe my syntax is off...) to search for in a string. If someone would help out, I'd appreciate it! Also, how would you...
7
by: Amy | last post by:
I have a string say a = "Hello how are YOU" I want to end up with a = "hello how are you' Isn't there a built in method for changing a string to lowercase?
11
by: John Velman | last post by:
I've used perl for a lot of 'throw away' scripts; I like Python better in principle, from reading about it, but it was always easier to just use perl rather than learn python. Now I'm writing a...
37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
4
by: Harro de Jong | last post by:
(absolute beginner here, sorry if this seems basic) Section 7.10 of 'How to Think Like a Computer Scientist' contains this discussion of string.find and other string functions: (quote) We can...
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
15
by: Optimus | last post by:
I would like to know if there is a encryption algorithm that returns only lowercase encrypted string. Thanks in advance.
1
by: mkk572ny | last post by:
Hi, I was wondering if anyone could help me to create a java method to changing the uppercase letters to lowercase and visa versa in a string using a for-loop. Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.