我试图解密一个句子。对于找到的每个单词,我都想将其添加到计数器中。我有一个嵌套for循环,当用户键入句子时,一个for循环将循环遍历句子(int i = 0),另一个(int j = 0)循环遍历数组,并且当它们找到相应的字母。我认为我所做的是有道理的,但由于某种原因,它不起作用。这里有一段代码处理本节。预先感谢您:)如何使用数组查找字符串中的字母C++
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string alphebet[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
string sentence;
cin >> sentence;
for(int i = 0; i < sentence.length(); i++){
for(int j = 0; j < alphebet; j++){
if (sentence[i] == alphebet[j]){
counter_letters = counter_letters + 1;
}
}
}
请检查[MCVE]。你的问题是什么? “它不工作”不通知,所以我们不能帮助。 –
'j'从'0'到'alphabet',你可能想'0'到'26' – user463035818
有一个'isupper'函数:https://linux.die.net/man/3/isupper – mch