Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Converting a word to lower case (http://www.programmingforums.org/showthread.php?t=13296)

357mag Jun 7th, 2007 1:03 AM

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.

DaWei Jun 7th, 2007 8:28 AM

Decase your words a char at a time (loop on word length) using tolower () or toupper ().

Infinite Recursion Jun 7th, 2007 9:31 AM

:

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);

DaWei Jun 7th, 2007 10:35 AM

or, more efficiently (no function call in the loop): ;)
:

...
    int len = source.length ();
    for (i = 0; i < len; ++i)
...


Cache Jun 7th, 2007 10:50 AM

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);


Infinite Recursion Jun 7th, 2007 12:13 PM

ooops, i should probably take the time to optimize the code in my lil' lib one day. :)

DaWei Jun 7th, 2007 1:52 PM

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.


All times are GMT -5. The time now is 2:35 AM.

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