编写程序,输入两个字符串存入数组a和数组b中,判断数组b中字符串是否在数组a字符串中出现(称为子串)。

2024-12-29 08:40:54
推荐回答(1个)
回答1:

#include
#define MAXLINE 100
int strindex(char source[ ], char searchfor[ ]);
char pattern[] = "ould"; /*要查找的模式*/

/* 找出所有与模式匹配的行*/
int main (void)
{
char *line[MAXLINE] = {
"How could you do that!",
"You cann't do that!",
"Would you like to do that?",
"No, I don't.",
NULL
};
int i = 0;

while (line[i])
if ( strindex(line[i++], pattern) >= 0 ) {
printf( "Yes\n");
}
else
printf ("No\n");
return 0;
}
/* strindex:返回t在s中的位置,若未找到则返回-1 */
int strindex(char s[], char t[] )
{
int i, j, k;
for ( i = 0; s[i] != '\0'; i++ ) {
/*这一行是关键,找到第一个字母后继续向后遍历到t末尾结束*/
for ( j =i, k = 0; t[k] != '\0' && s[j] ==t[k]; j++, k++ )
;
if ( k > 0 && t[k] == '\0' )
return i;
}
return -1;
}