-1

How do you accept case-insensitive and allow embedded blanks in a user input? So the user can enter “hong konG” and get a correct match to the input.

I only have the input[0] = toupper(input[0]); which only accepts if the case sensitive is at the beginning of the word.

while(true){
cout << "Enter a city by name: "<< " "; 
std::getline (std::cin,input);
if (input == "quit")
    {
        break;
    }
input[0] = toupper (input[0]);

    //....how do I loop to find all letter's in the input string variable?    
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
V Kid
  • 117
  • 4
  • 12
  • When you want to do something multiple times (like upper case each character of a string) you write a loop. So write a loop that uses toupper on each character. If you showed a little more of your code I could probably show you exactly how to do that. Only one line of code is not very much to work with. – john Oct 09 '13 at 16:05
  • And when you want to write a loop, you realize there's a clearer algorithm for it. The one for this is `std::transform`. – chris Oct 09 '13 at 16:10
  • 1
    @chris I'm an old-timer but I've never found loops to be unclear. – john Oct 09 '13 at 16:11
  • @john, Although an algorithm has a name attached, which should instantly give you a sense what it's doing by reading one word, plus you don't reinvent what's already made and optimized. I'll admit a ranged-for, in its brevity, also makes it really clear and doesn't have boilerplate `begin` and `end` calls to use. – chris Oct 09 '13 at 16:18

3 Answers3

4

You can use a loop to convert the entire string to upper case one character at a time, but a better solution is to use C++ standard library's transform function for that:

std::string hk = "hong konG";
std::transform(hk.begin(), hk.end(), hk.begin(), ::toupper);

This would apply ::toupper to all characters of your string, resulting in a string that reads "HONG KONG".

Demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2
for (auto& c : str)
    c = std::toupper(c)
David G
  • 94,763
  • 41
  • 167
  • 253
0

You can convert the whole string to upper-case like this

for (size_t i = 0; i < input.size(); ++i)
    input[i] = toupper (input[i]);

The other suggestion to use std::transform is also a perfectly good solution.

john
  • 85,011
  • 4
  • 57
  • 81
  • it's not working, when I add those above to the code, it's not "cout" the code back out. – V Kid Oct 09 '13 at 16:19
  • 1
    Maybe you made a mistake somewhere, hard to tell without seeing your code. – john Oct 09 '13 at 16:21
  • I don't want to change all the strings to uppercase, I just want to make sure that if someone enters in "konG" the output will still come out to be "kong". – V Kid Oct 09 '13 at 18:29
  • I'm trying to match the input let's say to a list of array i have, if someone enter's "konG" and on my array of list i have "kong" the input will still match the one on the list. – V Kid Oct 09 '13 at 18:29
  • Are all the strings on your list lower case? If so then convert the input string to lower case (use tolower not toupper). If you have a mixture of cases in your list then convert both the input string and the list string to the same case before you compare. I think you've been given the tools to solve this, you just have to think through what you are doing. – john Oct 09 '13 at 18:32
  • This code potentially invokes UB for arbitrary user input, see https://stackoverflow.com/q/21805674/3002139 – Baum mit Augen Aug 04 '17 at 20:59