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

若b串是a串的子串则输出“yes!”,否则输出 “no!” 高手们最好在比较绕弯的地方给点讲解! 谢谢啦

第1个回答  2011-04-12
#include <stdio.h>
#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;
}追问

高手 的确是高手 我这个才学到 数组 有些函数看不懂啊 有没有适合我这水平的?

追答

如果没学指针,那么测试用的字符串就用char a[MAX]; scanf("%s", a);这种方式读入。
关键的查找子串其实就用了数组前面的知识阿,都没有用指针,仔细理解一下,你肯定能明白的。

追问

好的好的 谢谢您的帮助啊!!!

追答

怎么没消息了?没有追问也没有采纳?

本回答被网友采纳