Thread: converting strings to lowercase

  1. #1

    Question converting strings to lowercase


    This is my first time programming with c++, so please bear with me.

    I have a string variable that needs to be converted into lower case or upper case. I want to use the tolower() or toupper() functions in the C string.

    I have a problem changing the string into a character array.

    This is my code:

    #include <iostream>
    #include <string>
    #include <ctype.h>
    using namespace std;

    int main( int argc, char *argv[] ) {
    string strMyString;
    string strNewString;

    cin >> strMyString; //pass in string to be changed

    char tmpChar[strMyString.length()];
    tmpChar = strMyString.c_str();

    for (int i = 0; i<strlen(tempChar); i++)
    { strNewString += tolower(tmpChar[i]);

    cout << strNewString << endl;

    i have a problem with assigning the character array tempChar to strMyString.c_str();

    does anyone have any ideas?

    thanks a lot!!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    you are mixing tmpChar and tempChar, two separate and distinct variables.

  3. #3
    C strings are a bit trickier than STL strings. First of all in the declaration of tmpChar the value in the []s needs to be a constant available at compile time. Since the length of strMyString is unknown at compile time the mechanism you used is in error. Even if it wasn't the length() method of STL strings returns the number of non-null characters in the string. To declare a C string to hold all the non-null char of the STL string you need to add space for the terminating null character in addition to all the non null char so it would be strMyString.length() + 1. AT least you attempted to declare memory for tmpChar, which is more than a lot of times.

    To declare just enough memory for tmpChar you would need to use dynamic memory like this;

    char * tmpChar = new char[strMyString.length() + 1];

    Alternatively, since I suspect you are not familiar with dynamic memory yet, you could declare tmpChar to hold as many char as conceivably necessary using static memory, like this:

    char tmpChar[256];

    you could replace 256 by 80 or 1000 or whatever you thought was as big as necessary.

    Next, you can't assign one C string to another using the = oprerator like you can two STL strings. You need to use the strcpy() (or similar) function found in cstring header file (or string.h header depending on your compiler). It works like this:

    char tmpChar[256];
    //obtain strMyString like you did
    strcpy(tmpChar, strMyString.c_str());

    last but not least, tempChar is not declared, though I suspect that is a typo for tmpChar.

    I believe you can convert tmpChar to strNewString like this:

    string strNewString(tmpChar);

    or like this:

    strNewString = string(tmpChar);

    I also believe that the STL string class has it's own built in toupper() and tolower() functions, although the names may be slightly different, to do all this for you if you wish.

  4. #4


    Thanks a lot for clearing up all these issues for me!
    I really appreciate it.....and as someone who has just started with c++, this really helps a lot.

    sorry about the typo: tempChar is tmpChar

    I couldn't find a similar method in the STL string reference.
    This is the reference i'm using:

    Thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting strings to char
    By Furious5k in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2009, 05:26 PM
  2. Converting strings to chars
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:17 AM
  3. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM