/* This program demonstrates * 1. strcmp - with our own implementation * 2. arrays of c-strings - an introduction * * * A note on ASCII: * In C++, char is both an integer type and a character type. This is because * C++ stores a char as an integer but interprets it as a character when it is * printed. * Since the days of telegraph, we have had the American Standard Code for * Information Interchange (ASCII), which assigns a numeric (integer) value for * every character that can be typed in. We have a numeric equivalent for every * uppercase letter, lowercase letter, numeral character, special character, * whitespace, control characters, etc. * When we store a character in a char variable (or a spot in a C-String), we * actually store the ASCII value of the character. * * An array of chars is a C-String. We can treat it as a regular array * along with all the special properties that apply to C-strings alone * Naturally, we can extend this idea into an array of C-Strings, which is * an array of arrays of chars - a 2 dimensional array of chars. * Here, each array of chars (an entire row of the array) can be treated as * a CString. Then, we only need to specify the row for the array. * We can also refer to each character individually. Then, we need to specify * both the row and the column. */ #include #include using namespace std; int stringCompare (char st1[], char st2[]); int main() { char str[100]; char str2[100]; cout << "Enter 2 strings: "; cin.getline(str, 100); cin.getline(str2, 100); /* Lexicographic ordering: * space < numeral (0-9) < uppercase letters (A-Z) < lowercase letters (a-z) * Spongebob, Patrick -> Patrick is "first" P < S * Spongebob , patirck -> Spongebob is "first", S < p * Spongebob, 4atrick -> 4atrick is "first", numeral < letters * Spongebob, Sqiudward -> Spongebob is "first" S==S, p Square is first, first 3 letters are the same, a < i * Squid, Squidward -> Squid is "first", all letters matched until "Squid" ran out of letters * Squid Squid -> strings are equal */ cout << "Demonstrating strcmp for string comparison:\n"; int diff = stringCompare(str, str2); if (diff < 0) cout << str << " was first" << endl; else if (diff > 0) cout << str2 << " was first" << endl; else cout << "The two strings were equal" << endl; cout << "Demonstrating our version of strcmp:\n"; diff = stringCompare(str, str2); if (diff < 0) cout << str << " was first" << endl; else if (diff > 0) cout << str2 << " was first" << endl; else cout << "The two strings were equal" << endl; /* An array of C-strings is essentially a two dimensional array of chars, where * each "row" can be considered a CString on its own. * So, we can treat is like a 2D array of chars, and all concepts that apply to * 2D arrays in general still apply. * But, we can also use the 4 extra "features" we added to c-strings (direct initialization, * direct reads, direct prints and null termination) to each row of the 2D array. */ char list[10][20] = {{'A','p','p','l','e'}, "strawberry"}; cout<<"Access individual character: "< uppercase characters -> lowercase characters * This is because we compare the ASCII values. * strcmp(string1, string2) * string comparison moves through both strings one character at a time, * and subtracts the ASCII value of the character of the second string * from the character of the first string. * The moment it sees a non-zero result on the subtraction, it returns that * So, strcmp returns * value < 0 if the first string occurs before the second string * value > 0 if the second string occurs before the first string * 0 if the strings are exactly equal (including case, spaces, etc.) */ int stringCompare(char st1[], char st2[]) { int diff = 0; int i=0; int len1 = strlen(st1), len2 = strlen(st2); while( i< len1 && i