Skip to content

Instantly share code, notes, and snippets.

@immmdreza
Created January 6, 2023 11:59
Show Gist options
  • Save immmdreza/81818afb6af386b378cbe2c51ce6f5cd to your computer and use it in GitHub Desktop.
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.
#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