![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Converting a word to lower case
I've got this program that compares two words. Later on it changes the case of a word from uppercase to lowercase. I saw someplace on the web where a guy was doing this:
string lowerOrangeFruit = ConvertToLowerCase(orangeFruit) He was including some header file called "strutils.h" and "genlib.h" I tried it in my program but my compiler says no way. Here is what I'm trying to do: int main()
{
string guitar = "guitar"; // g has an ASCII code of 103
string piano = "Piano"; // P has an ASCII code of 80
cout << "Does " << guitar << " come before " << piano << "?" << endl << endl;
if (guitar < piano)
cout << "Yes!";
else
cout << "No!";
string lowerPiano = string.tolower(piano);
return 0;
}But I'm doing something wrong. I sense that it must be a pretty easy fix. I'll have to keep monkeying around with it. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Decase your words a char at a time (loop on word length) using tolower () or toupper ().
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
string MyToLower (string source)
{
int i = 0;
string temp = "";
for (i=0;i <= source.length()-1; i++)
{
temp = temp + (char)(tolower(source[i]));
}
return temp;
}.... string lowerPiano = MyToLower(piano);
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
or, more efficiently (no function call in the loop):
...
int len = source.length ();
for (i = 0; i < len; ++i)
...
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
or, using the tools already provided by the standard:
#include <cctype> #include <string> #include <algorithm> // ... std::string str = "ABCDE"; std::transform(str.begin(), str.end(), str.begin(), std::tolower); |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
ooops, i should probably take the time to optimize the code in my lil' lib one day.
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
LOL, I don't really call it optimization. When I optimize, I put an analyzer on the thang and get a good profile. It just comes from being old and having to deal with 2K-256K of memory and a 1MHz-8MHz clock. One whittled as they went, and if it still didn't woik, they got serious and whittled some more. Mailpieces and such, moving at 100-400 inches per second, wait on no man to collect the data and decide when to divert their path.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VB Phone Number to Name... | NDawg28 | Visual Basic | 9 | Mar 10th, 2007 9:40 PM |
| need help with code plz | ELHEK | Java | 5 | Oct 1st, 2006 9:20 PM |
| BH Hotkeys | brownhead | Visual Basic | 1 | Apr 27th, 2006 5:42 PM |
| my Calculator | layer | Other Scripting Languages | 4 | Mar 9th, 2005 7:09 PM |