0

I'm reading in user input via

string word; while(cin >> myWord) //put myWord in array 

but for the sake of sorting I want "This" and "this" to be the same. For my sort algorithm I'm using the default < and > values for string, so This != this, and for my purposes I need it to == so I want to just immediately make everything lowercase while it's read in. From the tolower I see I would need to make a for loop to iterate through word and make it lowercase, so I'd need to do this inside of the while loop before putting the word into the array. But I'm wondering if there's any trick I can do to make put the word from cin into myWord already lower case (or make "myWord" lower case right after being read in) in one line, along the lines of cin >> myWord.lower is what I'm talking about

7
  • possible duplicate of How to convert std::string to lower case? Commented Nov 21, 2014 at 23:22
  • I'm not sure about how to do it in the same step but you could call tolower before putting it in the array to make sure it is lowercase. Commented Nov 21, 2014 at 23:23
  • I would change the sorting order in std::sort via a custom SortPredicate that compares strings ignoring the case. Commented Nov 21, 2014 at 23:25
  • Run your input through a preprocessor: tr A-Z a-z | my-program Commented Nov 21, 2014 at 23:30
  • @TommyK Add the word as-is and not change the case. When you call std::sort, that is where you compare without case. Commented Nov 21, 2014 at 23:43

2 Answers 2

0
std::for_each(myWord.begin(),myWord.end(),[] (char &c) {c=std::tolower(c);}); 
Sign up to request clarification or add additional context in comments.

2 Comments

If that's what the OP needs, it works perfectly, although I had the impression he wants to sort the array of strings ignoring the case.
@vsoftco I found it a bit confusing too. He wrote he wants instant transformation to lower case. And this one liner is very instant I hope. In case he wants to preserve caseness and only changes sorting order he definitely should go with your costum comparator.
0

That's how I would do it, by using a custom sorting predicate for std::string

#include <algorithm> #include <iostream> #include <locale> #include <string> struct SortNoCase // functor used to sort strings ignoring the case { bool operator()(const std::string& lhs, const std::string& rhs) const { std::string lhs_lower, rhs_lower; std::transform(std::begin(lhs), std::end(lhs), std::back_inserter(lhs_lower), ::tolower); std::transform(std::begin(rhs), std::end(rhs), std::back_inserter(rhs_lower), ::tolower); return lhs_lower < rhs_lower; } }; int main () { std::vector<std::string> vs{"Some", "strings", "THAT", "are", "UnSorted"}; std::sort(std::begin(vs), std::end(vs), SortNoCase()); for(auto&& elem: vs) std::cout << elem << " "; std::cout << std::endl; } 

PS: there are more sophisticated approaches, like using custom char traits, but this does it and it's easy to understand. If you're really curios, can take a look here:

https://stackoverflow.com/a/5319855/3093378

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.