0

in this code i'm comparing between two strings i did it correctly, but i don't want to consider the letters' case.

for ex: first string: aaaa, second string: aaaA. the output should be 0 or equal.

is there any ideas to fix this?

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() 
{
    cout << "enter the first string" << endl;
    string x; 
    cin >> x;
    cout << "enter the second string" << endl;
    string y; 
    cin >> y;
    cout << endl;
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    cout << x << endl;
    cout << y << endl;
    if (x.compare(y) < 0)
    {
        cout << "-1" << endl;
    }
    if (x.compare(y) > 0)
    {
        cout << "1" << endl;
    }
    if (x.compare(y) == 0)
    {
        cout << "0" << endl;
    }

} 
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Osama Nagi
  • 1
  • 1
  • 1
  • You could transform both input strings to lowercase prior to comparison; see e.g. [How to convert std::string to lower case?](https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case). – dfrib Jun 16 '20 at 07:30
  • @dfri No, that is a **bad solution**. It will fail for some international strings. – Konrad Rudolph Jun 16 '20 at 07:31
  • @KonradRudolph Ah I was not aware this was the case for `std::basic_string`, thanks. – dfrib Jun 16 '20 at 07:33
  • lowercase 2 strings before comparing them. – Hieu Doan Jun 16 '20 at 07:33
  • @dfri My concern is unrelated with `std::basic_string`, it applies to *every* string type in *every* programming language. Admittedly C++ strings are even worse since `tolower` and `toupper` are broken. – Konrad Rudolph Jun 16 '20 at 07:35

2 Answers2

1

You can use std::tolower to convert the strings x and y to its lower case representation and than compare the two lowercase strings.

#include <algorithm>

...
if (std::tolower(x) == std::tolower(y)) { 
    ...
}
...
schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • Don’t use `tolower` or `toupper` to implement case insensitive compare. This will fail for `x = "STRASSE"` and `y = "straße"`, which should compare equal. – Konrad Rudolph Jun 16 '20 at 07:36
  • 1
    Why should "STRASSE" and "straße" are equal? "SS" is not equal to "ß". – schorsch312 Jun 16 '20 at 07:57
  • Because that’s what the rules of German orthography say (DIN 5007-1). – Konrad Rudolph Jun 16 '20 at 08:02
  • That is wrong. The word "Strasse" does not exist in the German language. "Straße" is correct. https://www.korrekturen.de/beliebte_fehler/strasse.shtml Currently, an uppercase "ß" has been introduced. https://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F – schorsch312 Jun 16 '20 at 08:08
  • That’s all pretty irrelevant. String comparison (generally) doesn’t limit itself to “existing” words (for a given definition of “existing”), it controls string collation. Otherwise you could also say that “straße” isn’t a word in German, only “Straße” is. Yet if you walk through your town you should notice that the spelling “STRASSE” *does* exist in actual fact, even in highly official artefacts. – Konrad Rudolph Jun 16 '20 at 08:18
  • The OP asked for a case insensitive string comparison. What you are mentioning is a semantic comparison. – schorsch312 Jun 16 '20 at 08:22
  • No. I am *not* talking about “semantic” comparison, I’m talking purely about (case insensitive) string comparison, semantics play no role. This is all well-defined in Unicode, but C++ does not implement it natively. You need to use a library that handles strings correctly. Strings in C++ are merely byte sequences, not correct character sequences (despite the misleading data type name). – Konrad Rudolph Jun 16 '20 at 08:25
  • 1
    "straße" is indeed a bad example; yet, there's a locale-specific bug-o-feature with Unicode and Turkish locale where "I" (same char code as Latin) would lowercase to "ı", and "İ", to "i" (again, same as Latin), if your OS locale is Turkish. So tolower("Istanbul") == tolower("istanbul") would be false with Turkish OS locale and true, otherwise. – Andy Kovtun Jan 31 '23 at 22:44
-1

Solution from here

if (std::toupper(x) == std::toupper(y)) {
    cout << "0" << endl;
}
SolvedForHome
  • 152
  • 1
  • 15