Created
January 6, 2023 11:59
-
-
Save immmdreza/81818afb6af386b378cbe2c51ce6f5cd to your computer and use it in GitHub Desktop.
Alphabetically compare 2 words. 0 if they're equal, 1 if the first one should come sooner else 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
char getVerChar(char target) | |
{ | |
if (target >= 97 && target <= 122) | |
{ | |
return target - 32; | |
} | |
return target; | |
} | |
int compareNames(char name_1[20], char name_2[20]) | |
{ | |
char char_from_1; | |
char char_from_2; | |
char char_from_1_ver; | |
char char_from_2_ver; | |
for (size_t i = 0; i < 20; i++) | |
{ | |
char_from_1 = name_1[i]; | |
char_from_2 = name_2[i]; | |
char_from_1_ver = getVerChar(char_from_1); | |
char_from_2_ver = getVerChar(char_from_2); | |
if (char_from_1_ver == '\0') | |
{ | |
if (char_from_2_ver == '\0') | |
{ | |
return 0; | |
} | |
else | |
{ | |
return 1; | |
} | |
} | |
else if (char_from_2_ver == '\0') | |
{ | |
if (char_from_1_ver == '\0') | |
{ | |
return 0; | |
} | |
else | |
{ | |
return 2; | |
} | |
} | |
if (char_from_1_ver == char_from_2_ver) | |
{ | |
if (char_from_1 == char_from_2) | |
continue; | |
else if (char_from_1 < char_from_2) | |
return 1; | |
else | |
return 2; | |
} | |
else if (char_from_1_ver < char_from_2_ver) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 2; | |
} | |
} | |
return 0; | |
} | |
int main() | |
{ | |
char name_1[20] = "Abcc"; | |
char name_2[20] = "Abca"; | |
// Returns 0 is they're same, 1 if first is lower else 2. | |
cout << compareNames(name_1, name_2); | |
// 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment