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

Tommy K
  • 1,759
  • 3
  • 28
  • 51
  • possible duplicate of [How to convert std::string to lower case?](http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case) – bialpio Nov 21 '14 at 23:22
  • I'm not sure about how to do it in the same step but you could call [tolower](http://www.cprogramming.com/fod/tolower.html) before putting it in the array to make sure it is lowercase. – John Odom Nov 21 '14 at 23:23
  • I would change the sorting order in `std::sort` via a custom `SortPredicate` that compares strings ignoring the case. – vsoftco Nov 21 '14 at 23:25
  • Run your input through a preprocessor: `tr A-Z a-z | my-program` – William Pursell Nov 21 '14 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. – PaulMcKenzie Nov 21 '14 at 23:43
  • Tommy, I think it's a bit unclear, are you trying to sort string, or the vector of strings? – vsoftco Nov 21 '14 at 23:45
  • Or just change sorting order but keep the caseness of the strings ? – Oncaphillis Nov 21 '14 at 23:53

2 Answers2

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

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0
std::for_each(myWord.begin(),myWord.end(),[] (char &c) {c=std::tolower(c);}); 
Oncaphillis
  • 1,888
  • 13
  • 15
  • 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 Nov 21 '14 at 23:46
  • @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. – Oncaphillis Nov 21 '14 at 23:52