I have a vector<string> that I'd like to make case insensitive i.e.
(a) strings that always appear with first character upper-case, like proper nouns, should remain unchanged but,
(b) if a string appears with upper case first character that when replaced with a lower case of the same character has another matching string in the vector, then the lower case first character version of the string should be retained.
So, in short, if my vector<string> is ...
Code:
{"at", "Google", "At", "holiday", "Google", "experiment"}
... the program output should be:
Code:
{"at", "Google", "at", "holiday", "Google", "experiment"} //Assume string.length() > 1, for now, for all strings;
My psuedo-code for this runs on the following lines - for each vector element:
1. check if element[0] is uppercase; if not uppercase then continue to next vector element;
2. if element[0] is uppercase, convert this element[0] to lowercase and add to it the remaining sub-string - assign this to the original element and check if there is any match of this modified vector element with any other vector elements
3. if no match, convert element[0] back to upper case, add to it the remaining sub-string, this is now the new new vector element which is == the original vector element and move to next vector element
4. if match, move to next vector element
Based on the above lines of thinking, my program is as follows:
Code:
#include<iostream>
#include<vector>
#include<locale>
#include<algorithm>
#include<string>
using namespace std;
int main(){
vector<string> v {"at", "Google", "At", "holiday", "Google", "experiment"};
for(auto& itr:v){
if( (isupper(itr[0])) != 0){
itr = tolower(itr[0], locale()) + itr.substr(1);
if(!(find(v.begin(), v.end(), itr) != v.end())){
itr = toupper(itr[0], locale())+itr.substr(1);
}
}
}
for(auto& itr: v){
cout<<itr<<"\n";
}
}
However, the program output is:
Code:
{"at", "google", "at", "holiday", "google", "experiment"}
Could someone please let me know where exactly might be the problem in the psuedo and/or actual code? Many thanks!