as above.

string s;
cin >> s;

for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.


thanks in advance

There is a standard function called tolower() (and a companion function called toupper()) that can be used to evaluate each char of a string one at a time and change it to the appropriate case as needed. So to use it, look up tolower() in your compilers help section and make sure you include any necessary standard headers. Then obtain input string and determine it's length. Then loop through the string looking at each character one at a time, passing each char to tolower() as you go

inputString = tolower(inputString);

as above.

string s;
cin >> s;

for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.


thanks in advance

Here is a function that'll do it:

string UpToLow(string str) {
    for (int i=0;i<strlen(str.c_str());i++) 
        if (str[i] >= 0x41 && str[i] <= 0x5A) 
            str[i] = str[i] + 0x20;
    return str;
}

Good luck, LamaBot

Go with lerner's idea.

Lazaro Claiborn's idea, regardless of whether it works or not, is rather obfuscated.

It also has other problems...

http://www.cprogramming.com/tips/showTip.php?tip=59&count=30&page=0

Umm... obfuscated???? It is rather easy to understand, which is why I wrote it like that. Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps. However, it is not obfuscated. Let me explain what the code does:

You pass the string you want to be converted to all lower case, which is what the OP specified
It loops and iterates through dealing with each element of the string
If the elements corresponding hex value is not in between the values within the capital alphabetcial letter range defined by ascii, it won't do anything (i.e won't convert numbers)
If it is within range it adds 20 using hex notation (i.e 0x20) from its corresponding hex value - which effectively converts it to its lowercase counterpart.

That is very easy to understand to say the least.

LamaBot

Member Avatar for iamthwee

If you say so. But you wouldn't give a newbie that.

>Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps.

Try reading my link again. Then you might realise what you're doing wrong. :)

If you say so. But you wouldn't give a newbie that.

>Actually, strlen(str.c_str()) could be replaced with, say "str.length" perhaps.

Try reading my link again. Then you might realise what you're doing wrong. :)

Well that'd be the problem if the string contained a massive amount of characters - which you nor I know. I read the link you'd posted and I still don't realise the problem. Lol. :lol: I realised that might be problem therefore I suggested "str.length" as alternative. Overall, if he's a newbie trying to learn he probably doesn't care, yet, the specific operations of a function, such as strlen(...); not to imply it doesn't matter, becuase code optimization is vital to a programs performance. You have to consider the obvious.

Good luck, LamaBot

Member Avatar for iamthwee

str.length() <-- Brackets

:lol:

str.length() <-- Brackets

:lol:

str.lenght() <-- Enclosing Parenthesis

:lol: :lol: :cheesy:

[[ <--- Brackets
[ <--- Opening Brackets
] <--- Closing Brackets
[] <--- Enclosing Brackets


LamaBot

Member Avatar for iamthwee

str.lenght() <-- Enclosing Parenthesis

Er not were I'm from. All I was pointing out was you forgot them when u said use str.length.

The main thing is you have learnt why it is a bad idea to use strlen() in a for loop and why it is bad to give newbies non-intuitive code as examples. So overall, everyone is happy. Yay.

:lol:

str.lenght() <-- Enclosing Parenthesis

Er not were I'm from. All I was pointing out was you forgot them when u said use str.length.

The main thing is you have learnt why it is a bad idea to use strlen() in a for loop and why it is bad to give newbies non-intuitive code as examples. So overall, everyone is happy. Yay.

:lol:

I have written pages for my site regarding that issue. To you it is not intuitive. Unless the newbie has a very large string, using strlen isn't a truely bad thing to do; because it will not have to loop many times through each element to get the legnth each time the for loop, loops. I was just being a smartass as you were being - so it seemed. But yes, overall everyone is happy.

Last post, LamaBot

as above.

string s;
cin >> s;

for example, if the user enters Orange, i want it to convert to orange.
please advise what i should do.


thanks in advance

Check out this code

std::string str;
cin>>str;
transform(str.begin(), str.end(),str.begin(), tolower );
cout<< str;

I have written pages for my site regarding that issue. To you it is not intuitive. Unless the newbie has a very large string, using strlen isn't a truely bad thing to do; because it will not have to loop many times through each element to get the legnth each time the for loop, loops.

Programmer's don't take anything for granted. The usual "the string can't be that long" thing just doesn't cut ice in a practical setting. A piece of code which makes repeated calls to a function inside a loop will always perform worse than a function which doesn't, no matter how forgiving the compiler is.

I was just being a smartass as you were being - so it seemed.

*ahem*

Programmer's don't take anything for granted. The usual "the string can't be that long" thing just doesn't cut ice in a practical setting. A piece of code which makes repeated calls to a function inside a loop will always perform worse than a function which doesn't, no matter how forgiving the compiler is.

*ahem*

I totally agree with you but a newbie would be learning the concepts rather than the specifics. In many beginning programming books and classes they'd probably teach the concepts of programming instead of jumping right into the specifics of how a standard library function works. After a newbie writes enough programs and knows enough he can start considering code modifications, techniques and methods. I mean, in my opinion, if I were to start learning programming I wouldn't want to be told this is not good programming becuase of this.... I would just want to get the program to work so that I can get a better understanding of the concept (i.e. Conditional loops) better. After a while he or she might say, "I'm the master at Conditional loops, now, how can I modify it so that it reduces the amount of commands executed each loop".

LamaBot

In that case, it would be a good idea to mention at the beginning that the given method is not the best and there are better alternatives since beginners are known to grasp both good and bad equally well.... ;)

thank you everybody. Problem resolved with Lerner's method. BTW, below is my full program

#include <iostream>
#include <string>
using namespace std;

int main (void) {
    //char *fruit[] = {“apple”, ”banana”, “melon”, “orange”, “pear”};
    int stock[] = {32, 10, 30, 50, 25};
    bool available = 0;
    //cout << fruit[0];
    string s[] = {"apple","banana","melon","orange","pear"};
    string i;
    cout << "Enter the name of fruit\n";
    cin >> i;
    for (int q=0; q<i.length(); q++) {
        i[q]=tolower(i[q]);
    }
    for (int k=0; k<5; k++) {
        if (i==s[k]) {
            cout << stock[k] << "in stock";
            available=1;
            break;
           }
    }
    if (available==0)
    cout << "ERROR!!! Item entered is not available";
}

I like this part: tolower(i[q]); I've always wanted "to lower an IQ..." :mrgreen:

Very cute!

All I can say is wow... you guys sure do like to do things the hard way. The best approach is to leverage Boost! Why reinvent stuff? Works for Windows, Linux, Mac, Sun, HP, IBM AIX, VAX ... etc. And of course it's free.

#include <boost/algorithm/string.hpp>
use namespace boost;
string s;
cin >> s;
to_lower(s);

DONE!

commented: All I can say is, wow... don't dig up a two-year-old thread. -2

Check out this code

std::string str;
cin>>str;
transform(str.begin(), str.end(),str.begin(), tolower );
cout<< str;

By far the best non-Boost answer of the whole thread. The STL is powerful; use it! Looping over string as if it were a char* and downcasing each letter one by one is a "C" answer, not a C++ answer.

inline unsigned short int toLow(char const c)
{
   return (static_cast<unsigned short int>((c >= 'A' && c <= 'Z')?(c|0x20):c));
}
string strToLower(const string& str)
{
   string str2;
   for (string::const_iterator itr = str.begin();itr!=str.end();itr++)
      str2+=toLow(*itr);
   return str2;
}

Hey, guys, wake up it's 2010

c >= 'A' && c <= 'Z'

lol
How ignorant is it to assume your alphabet consists of a-z letters only... Please use tolower/toupper or STL/boost conversion routines, thay are aware of your locale.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.