3

I am fairly new to C++, working through Bjarne Stroustrup's "Programming and Practices using C++, 2nd Edition".

I am doing an exercise where you sort three names. I did this using the tools provided up to this point, so as not to jump ahead of the book. The method is to just use greater-than and less-than in an if statement.

I tried testing this with many inputs, and found something odd: When I use capital letters, it doesn't always sort properly. Can anyone explain why?

string name1, name2, name3;
cout << "Enter three names : \n";
cin >> name1 >> name2 >> name3;
if (name1 > name2)
{
    string temp;
    temp = name2;
    name2 = name1;
    name1 = temp;
}
if (name2 > name3)
{
    string temp;
    temp = name3;
    name3 = name2;
    name2 = temp;
}
if (name1 > name2)
{
    string temp;
    temp = name2;
    name2 = name1;
    name1 = temp;
}
cout << "In alphabetical order : \n" << name1 << '\n' << name2 << '\n' << name3;

The output of this test:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    Have you check you ASCII table lately? – Surt Feb 22 '21 at 23:26
  • 2
    Characters are just really small integer numbers. The mapping of characters to their integer values is defined by ASCII. The letter 'G' has value 71 and 'g' has value 103 - they are not the same thing. – Jerry Jeremiah Feb 22 '21 at 23:27
  • I am new to C++. I am an EE trying to learn so I am asking what I am missing here. In this book they show that you can compare words this way, but don't provide any nuance. So, to answer your question I haven't checked an ASCII table. Is that what the comparison is checking? Based on ASCII values? – Chrisplusian Feb 22 '21 at 23:28
  • Your book should explain that this is exactly how `std::string`s are compared with each other. If it doesn't, you might want to look for a more helpful book. – Sam Varshavchik Feb 22 '21 at 23:31
  • @Chrisplusian Is that the real code? The middle `if` doesn't even swap the strings. – dxiv Feb 22 '21 at 23:33
  • Thanks!! I get it now. To be fair the book tells you to create the code, and tells you exactly what to input to test. I always try to break anything like this. It is my opinion that you learn more that way. So far the book is great, but it doesn't take a typical approach. I chose this book case the Author is the creator of C++. – Chrisplusian Feb 22 '21 at 23:35
  • Each character in C++ (and commonly in compute science) has a numeric equivalent which is given by the ascii table: http://www.asciitable.com/ Now, as a beginner, this might be hard to find, but the comparison operator used is [here](http://www.cplusplus.com/reference/string/string/operators/), and described [here](http://www.cplusplus.com/reference/string/string/compare/). In your case: `str1 > str2` means "character of str1 is greater than str2 OR all characters are equal, but str1 is longer". – Cedric Feb 22 '21 at 23:35
  • Actually it isn't easy. There is no single operator/function method to do that with std::string like there is with C character arrays. You might want to look at https://stackoverflow.com/questions/28387362/is-there-a-built-in-function-for-stdstring-in-c-to-compare-two-strings-alpha and https://stackoverflow.com/questions/9182912/case-insensitive-string-comparison-c – Jerry Jeremiah Feb 22 '21 at 23:36
  • @dxiv good catch, I didn't even see that. I changed it and re-ran the code and it had different results. I think I understand now knowing that ASCII is what I should look at. – Chrisplusian Feb 22 '21 at 23:38
  • So then to summarize, this method of comparing strings for alphabetic order is very much lacking. Something which still doesn't make sense... if I was comparing these based on ASCII values, really all the characters would all need to be lower or upper case but not both. I am not sure the ASCII idea makes sense anymore. If there are three characters compared to four, do the characters get tested character by character? I realize the book didn't mean for me to dive so deep into this, but since I am learning on my own I have to ask. – Chrisplusian Feb 22 '21 at 23:54

1 Answers1

1

From an EE perspective, consider that strings in C++ (or C for that matter) are simply binary-encoded data within the machine.

Each character (letter, number, symbol) - is assigned an ASCII numeric value so the computer can interpret what character you are referencing.

The thing to understand here is that lower case (e.g. 'a') and uppercase letters (e.g. 'A') have different numeric values. Therefore, when all the characters in a string are compared, strings with different capitalisation result in different numeric values, hence the behaviour you are seeing.

Remember, to a human - you are comparing a string. To the machine, it creates a numberic representation of the string and compares it that way.

EDIT: Here is the reference for the 'compare' function, that the < and >

Alex Taylor
  • 7,128
  • 2
  • 26
  • 22