編寫函數strrindex(s,t),返回t在s中最左邊最早出現的位置,若s中沒有t的任意一個字符,則返回-1。
代碼:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
int strrindex(const char *s, const char *t);
int main(int argc, char *argv[])
{
char s[MAXLINE+1] = "hello world! this is a c program!";
char t[MAXLINE+1] = "kkjj0o";
printf("%d\n", strrindex(s, t));
return 0;
}
int strrindex(const char *s, const char *t)
{
for (int i = 0; s[i] != '\0'; ++i) {
for (int j = 0; s[j] != '\0'; ++j) {
if (s[i] == t[j]) return i;
}
}
return -1;
}