#include using namespace std; // C-string length, using array notation int length(char* stringOfInterest) { int length = 0; int i = 0; while (stringOfInterest[i] != NULL) { length = length + 1; i = i + 1; } return length; } // C-string length, using pointer notation int length2(char* stringOfInterest) { int length = 0; char* position; position = stringOfInterest; while (*position != '\0') { length = length + 1; position = position + 1; } return length; } // this implementation is similar to Jay's ideas from class bool equal(char* stringOne, char* stringTwo) { cout << "Running equality for " << stringOne << " and " << stringTwo << endl; int i,j; // NOTE THE TEST TO SEE IF WE GOT THE SAME POINTER (THAT'S AN AUTOMATIC TRUE FOR EQUAL) if (stringOne != stringTwo) { cout << "Checking characters: " << endl; i = 0; j = 0; while ((stringOne[i] != '\0') && (stringTwo[j] != '\0')) { // mismatch if (stringOne[i] != stringTwo[j]) return false; i = i + 1; j = j + 1; } // didn't get a mismatch character so test they are both on the null symbol if (stringOne[i] == '\0') { if (stringTwo[j] != '\0') return false; } else // already know stringOne is not on null symbol { if (stringTwo[j] == '\0') return false; } } else { cout << "Pointer Match!" << endl; } return true; // if make it here, they are matched } // this implementation is similar to Charlie's ideas from class bool equal2(char* stringOne, char* stringTwo) { cout << "Running equality (test 2) for " << stringOne << " and " << stringTwo << endl; int i,j; if (stringOne != stringTwo) { cout << "Checking characters: " << endl; i = 0; j = 0; // while they match and we haven't hit the end... while ((stringOne[i] == stringTwo[j]) && (stringOne[i] != '\0') && (stringTwo[j] != '\0')) { // keep moving up (we can reduce this even more (codewise) by using // pointer notation in the line above and the ++ that Charlie suggested in class i = i + 1; j = j + 1; } // at this point, know something has happened -- see what // their are four cases, but they minimize down to two // 1) you aren't the same string so two regular characters mismatched // 2) stringOne is shorter than the other, so the '\0' will mismatch against a real character // 3) stringTwo is shorter than the other, so same thing happens // -- these are all caught here -- if (stringOne[i] != stringTwo[i]) return false; // the other case is they are the same string, so just return true else return true; } else { cout << "Pointer Match!" << endl; } return true; } int main(int argc, char* argv[]) { int i; char* p = argv[1]; cout << "Program Name: " << argv[0] << " length(): " << length(argv[0]) << " length2(): " << length2(argv[0]) << endl; for (i = 1; i < argc; i++) { cout << "Argument " << i << ": " << argv[i] << " length(): " << length(argv[i]) << " length2(): " << length2(argv[i]) << endl; } if (equal(argv[1],argv[1])) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal(argv[1],p)) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal(argv[1],argv[2])) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal(argv[0],argv[2])) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal2(argv[1],argv[1])) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal2(argv[1],p)) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal2(argv[1],argv[2])) cout << "Match" << endl; else cout << "Mismatch" << endl; if (equal2(argv[0],argv[2])) cout << "Match" << endl; else cout << "Mismatch" << endl; return 0; }