-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? } 
4
  • 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. Commented Oct 9, 2013 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. Commented Oct 9, 2013 at 16:10
  • 1
    @chris I'm an old-timer but I've never found loops to be unclear. Commented Oct 9, 2013 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. Commented Oct 9, 2013 at 16:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what is the library for transform?
#include <algorithm>
This code potentially invokes UB for arbitrary user input, see stackoverflow.com/q/21805674/3002139
2
for (auto& c : str) c = std::toupper(c) 

1 Comment

This code potentially invokes UB for arbitrary user input, see stackoverflow.com/q/21805674/3002139
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.

6 Comments

it's not working, when I add those above to the code, it's not "cout" the code back out.
Maybe you made a mistake somewhere, hard to tell without seeing your code.
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".
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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.