请各位帮忙看看错在哪里。
输出 包含 字符串 pattern[]的行,并找出 字符串 pattern[]在输入行中最右边的位置。感觉是函数 strindex 返回值得问题,可是找不啊出。
程序代码:#include <stdio.h>
#define MAXLINE 1000 /*输入行长度*/
int getline(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[] = "ould"; /*待查找的行*/
//找出匹配的行
main()
{
char fine[MAXLINE];
int c ;
while(getline(fine,MAXLINE) > 0)
if(c= strindex(fine,pattern) >= 0)
printf("%s\t出现在%d",fine,c);
return 0;
}
int getline(char s[],int lim)
{
int i,c;
for(i = 0 ; i < --lim && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == 'n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int strindex(char s[],char to [])
{
int i,j,k,o;
for(i = 0; s[i] != '\0'; ++i){
for(j = i, k = 0; to[k] != '\0' && s[j] == to[k];++j, ++k)
if(to[k]=='\0')
o = i;
if(k > 0 && to[k] == '\0')
return o;
}
return -1;
}[ 本帖最后由 xjh110119 于 2012-3-3 18:49 编辑 ]






