C程序设计语言(第二版) 4-1
时间:2010-07-19 来源:carolaif
4-1 编写函数strrindex(s,t),它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1
#include<stdio.h>
#define MAXLINE 100
int getline(char line[], int max);
int strrindex(char s[], char t[]);
char pattern[] = "abc";//要查找的模式
int main()
{
char line[MAXLINE];
int found = 0;
while(getline(line,MAXLINE)>0){
found = strrindex(line,pattern);
printf("%s出现在最右边第%d个字符",pattern,found);
}
return 0;
}
int getline(char s[],int lim)
{
int c,i;
i=0;
while(--lim>0 && (c=getchar())!=EOF && c!='\n')
s[i++]=c;
if (c=='\n')
s[i++]=c;
s[i]='\0';
return i;
}
int strrindex(char s[], char t[])
{
int i,j,k,right;
j=0;
k=0;
right=0;
for(i=0;s[i]!='\0';i++){
for(j=i,k=0; t[k]!='\0' && s[j]==t[k];k++,j++)
;
if(k>0 && t[k]=='\0')
{
if(i>right)
right = i;
}
}
if(right==0)
return -1;
else
return right;
}
相关阅读 更多 +
排行榜 更多 +










