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"))
{
...
}
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.
Try this: - #include <cctype>
-
-
// cctype.h includes the method tolower(int), which will return the
-
// lowercase number value of the characters (represented by int).
-
// For example, if 'A' is 36 and 'a' is 65 (Random numbers, do not
-
// use these!), then tolower('A') will return 65.
-
-
char ch = static_cast<char>(tolower(otherCh));
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
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
ganon if i have a string "In" how would i apply that code then? after cin>> In;
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: -
string str("JAN");
-
int length = str.size();
-
for (int i = 0; i < length; ++i)
-
{
-
str[i] = tolower(str[i]);
-
}
-
There are other more C++-like ways to do this but this is OK for starters.
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. !!!
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. !!!
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
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.
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
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.
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
i think i posted this at 5am or after waking up lol i thought it was my original post, my bad!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
|
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...
|
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...
|
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;
|
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...
|
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'...
|
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.
|
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.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |