I have created a set of algorithms that takes an input of a string vector, checks whether any of the strings occur more than once: if so erases all additional occurrences of the string from the vector, then outputs the new, 'lighter' array without the redundancies.
It works great except now I am to make it case-insensitive; I am attempting to simply add the toupper()
std function to the ==
comparison statement, however it does not seem to work.
I have a more familiar background with Java and am trying to learn C++.
Can someone please show me how to correct my syntax?
// Output old list.
cout << endl << "==========\nOld list:\n==========";
for (int i = 0; i < count; i++) {
cout << endl << list[i];
}
cout << endl << endl;
// Check uniqueness.
for (int i = 0; i < count; i++)
for (int j = i+1; j < count; j++) {
if (toupper(list[i]) == toupper(list[j])) {
list[j] = "";
count--;
}
}
// Output new list.
cout << endl << "==========\nNew list:\n==========";
for (int i = 0; i < count; i++) {
cout << endl << list[i];
}
cout << endl << endl;
Your loop leaves "holes" in the list
array vector, but the size of the array vector does not change (but you decrease your upper bound count
)
There are probably many other alternatives, but if you don't want to modify it much, probably you need in an addtional loop to copy non-empty elements from the list
array into a new array
First we're going to have a function to do the toUpper (this is modified from @Jim22150)
std::string stringToUpper(const std::string &input) {
std::string toBeModified=input;
std::transform(toBeModified.begin(), toBeModified.end(), toBeModified.begin(), ::toupper);
return toBeModified;
}
Now, we must not leave holes, so we should use erase (as @Scott Christopher Stauffe indicated):
// Output old list.
cout << endl << "==========\nOld list:\n==========";
for (int i = 0; i < count; i++) {
cout << endl << list[i];
}
cout << endl << endl;
// Check uniqueness.
for (int i = 0; i < count; i++)
for (int j = i + 1; j < count; j++) {
if(stringToUpper(list[i]) == stringToUpper(list[j])) {
list.erase(j,1);
count--;
}
}
}
// Output new list.
cout << endl << "==========\nNew list:\n==========";
for (int i = 0; i < count; i++) {
cout << endl << newlist[i];
}
cout << endl << endl;