Case sensitive?

Sep 6, 2011 at 9:37am
closed account (967L1hU5)
I'm making a program where there is a lot of commands. To get a command from the user, I use:
1
2
string x;
getline (cin, x);

And that works fine and all, but I don't like that if the user types, say "action", that it would have a different effect than if the user typed "Action", or "aCtIoN". So my question is, is there a way to sort of convert strings to all caps, or no caps, so I would only have to program the one string, or any other way?
Sep 6, 2011 at 10:11am
You can use std::transform():
1
2
3
4
5
6
7
#include <string>
#include <algorithm>
#include <cctype>

std::string s = "SoMetHinG LiKe tHis";

std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(tolower));
Sep 6, 2011 at 10:13am
http://msdn.microsoft.com/en-us/library/system.string_methods.aspx

You can use the string library.

ToLower() Returns a copy of this string converted to lowercase.

ToUpper() Returns a copy of this string converted to uppercase.

Sep 6, 2011 at 1:55pm
Or you can use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
#include <string>

void Lower(std::string str&)
{
  
     for (int i = 0;  i < str.size(); i++)
    {
           str[i] = tolower(str[i]);
    }
}

int main ()
{
    std::string str = "SoMeThINg";
    Lowr(str);
    cout << str;
    return o
}


This will output:
something
Topic archived. No new replies allowed.