Search code examples
c++stringlowercase

How do you make a string lowercase (the easy way)? (C++)


I am building a program that takes input, then detects to see it a specific thing was entered. After the line was entered,it should convert it to lowercase. But, I cannot figure out how. I have tried tolower(), but I cannot figure out what to put in the brackets. Here is the code:

#include <string>
#include <sstream>
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
    while (1)
    {
        string dir = "C:/";
        string line;
        string afterPrint;
        string prompt = "| " + dir + " |> ";
        cout << prompt;
        cin >> line;

        stringstream ss(line);
        while (ss)
        {
            string command;
            ss >> command;
            command = ;// Command, but lowercase
            if (command == "cd")
            {
                string dir;
                if (ss >> dir)
                {
                    cout << "changedir: " << dir << "\n";
                    string prompt = "| " + dir + " |> ";
                }
            }
            else if (command == "c:" || command == "c:\\" || command == "c:/")
            {
                cout << "Directory: " << dir << "\n";
            }
            else
            {
                cout << "error\n";
            }
            break;
        }
    }
    return 0;
}

Help would be highly appreciated!


Solution

  • tolower works on single characters. Note that the documentation also states that non-alpha characters are passed through unmodified.

    This also looks like a good location to use std::transform.

    If you look in the examples for std::transform, there is an example of using std::toupper to transform a string. Try adapting that example for your needs.