Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2007, 12:03 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Jun 7th, 2007, 7:28 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 7th, 2007, 8:31 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jun 7th, 2007, 9:35 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 7th, 2007, 9:50 AM   #5
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
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);
Cache is offline   Reply With Quote
Old Jun 7th, 2007, 11:13 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jun 7th, 2007, 12:52 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC