做拼寫卡片時,要提醒小朋友哪裡有雙字母。寫 doubled(word):一個字和它前一個字一樣,就數一次。coffee 是 2,aaa 也是 2,空的是 0。
doubled("coffee")→2doubled("cat")→0doubled("book")→1int doubled(const string &word)#include <string>
using namespace std;
int doubled(const string &word) {
int count = 0;
for (int i = 1; i < word.size(); i++) {
// 在這裡寫:word[i] 和前一個字一樣,count 就加 1
}
return count;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。