69,369
社区成员
发帖
与我相关
我的任务
分享
//给定字符串:this is java for you,java is good for programming!
//打印最后一次出现字符串"for"的位置。
#include <stdio.h>
#include <string.h>
char s[]="this is java for you,java is good for programming!";
char f[]="for";
char *p,*q;
int main() {
p=strstr(s,f);
if (NULL==p) {
printf("Can not find '%s' in '%s'!\n",f,s);
} else {
q=p+strlen(f);
while (1) {
p=strstr(q,f);
if (NULL==p) break;
q=p+strlen(f);
}
printf("The last location from 0 of '%s' in '%s' is %d.\n",f,s,q-s-strlen(f));
}
return 0;
}
//The last location from 0 of 'for' in 'this is java for you,java is good for programming!' is 34.
//