✔ 最佳答案
/* string matching: strmatch.c
checks for matching of two strings in the initial portion
http://hk.knowledge.yahoo.com/question/question?qid=7008030801903
*/
#include
#include < stdlib.h>
#include < string.h>
int strmatch(char s1[], char s2[])
{
int i=0;
while(s1[i]!='\0' && s2[i]!='\0' && s1[i]==s2[i])i++;
return i;
}
int main(int argc, char *argv[])
{
char s1[200],s2[200],s3[200];
printf("string1:"); scanf("%s",s1);
printf("string2:"); scanf("%s",s2);
int nChar=0;
if((nChar=strmatch(s1,s2)) > 0)// matches at least one character
{
strcpy(s3,s1); s3[nChar]='\0';
printf("Matching initial portion is '%s'\n", s3);
} else {// no match
printf("No matching initial portion\n");
}
return 0;
}