// File: strcmpEx.cpp // Example of strcmp implemented with pointer arithmetic (no indexing) #include #include using namespace std; int strcmpPtr(char *str1,char *str2); int main(int argc, char *argv[]) { char s1[20],s2[20]; if (argc>1) { // Command line arguments present strcpy(s1,argv[1]); strcpy(s2,argv[2]); } else { cout << "Compare two strings and see result\nEnter first string >"; cin >> s1; cout << "Enter 2nd string >"; cin >> s2; } cout << "Our strcmp result is " << strcmpPtr(s1,s2) << endl; cout << "Result using strcmp is " << strcmp(s1,s2) << endl; } int strcmpPtr(char *str1,char *str2) { for ( ;*str1 && *str2;str1++,str2++) if (*str1 != *str2) return(*str1-*str2); return(*str1-*str2); }