練習4.1

編寫函數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;
}


 

發佈了51 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章