c program

2008-03-09 12:29 am
write a program to input two strings.print the matching initial portion,ifany.your program should produce smilar output as follows:
sample output
string1:grandfther
string2:grandmother
Mathching initial portion is'grand'
sample output
string1:father
string2:moher
No mathching initial portion

回答 (1)

2008-03-09 2:19 pm
✔ 最佳答案
/* 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;
}


收錄日期: 2021-04-13 15:15:26
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080308000051KK01903

檢視 Wayback Machine 備份