Here is code which compare s2 with s1, if they are the same it returns 0 and some other options, but while loop is not end-able and I can not find its problem, the only thing I know if I convert char* s2 into const char* s2 it will work fine.
#include <iostream>
using namespace std;
int cmp(char*,char*);
int main()
{
char* s1;
cout << "Please Enter First Word: "; cin >> s1;
char* s2;
cout << "Please Enter Second Word: "; cin >> s2;
cout << "The Result is: " << cmp(s1,s2) << endl;
return 0;
}
int cmp(char* s1, char* s2)
{
int i=0;
while (*(s2+i)!=0)
{
if (*(s2+i)>*(s1+i)) return 1;
if (*(s2+i)<*(s1+i)) return -1;
i++;
}
return 0;
}
Consider:
How big is
s1? The problem is that your char pointers doesn’t point to any memory block. You need to allocate memory withnew.