The main idea of the title: Implement the strcmp function by yourself. The function is to compare the size of the two functions. If s1>s2, return 1, if s1<s2, return -1, otherwise return 0
Topic analysis: Scan two character arrays from left to right respectively, and if the current two characters are found to be equal, move one bit back. When it is found that it is not equal or one reaches the end position, the scanning can be stopped and the size can be judged. Because the last bit of the character is '\0', which is smaller than any ASCII character, it does not need to be processed separately
Code display:
#include <iostream> #include <string> using namespace std; int myStrcmp(char *s1,char *s2){ int i=0,j=0; while(s1[i]==s2[j] && s1[i]!='\0'){ i++; j++; } if(s1[i]>s2[j]) return 1; else if(s1[i]<s2[j]) return -1; else return 0; } int main(){ char a[100],b[100]; cin>>a>>b; cout<<myStrcmp(a,b)<<endl; return 0; }