Kodewick
開始學 →
基礎←→0 / 70

是照片嗎

#042基礎strlen(file) - 4約 5 分鐘

整理相簿,只留照片。寫 is_jpg(file):檔名 file 以 .jpg 結尾就返回 true,否則 false。只認小寫;比 4 個字元還短的名字一定不是。

示例
is_jpg("beach.jpg")→True
is_jpg("notes.txt")→False
函式bool is_jpg(const char *file)
main.c
#include <stdbool.h>
#include <string.h>

bool is_jpg(const char *file) {
    bool ok = false;
    int len = strlen(file);
    if (len >= 4) {
        // 在這裡寫:從倒數第 4 個字元開始,和 ".jpg" 比
    }
    return ok;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。