谁能向我解释一下如何找到字符串单词的大写和小写字母?我需要知道单词是说“鱼”,“鱼”,“鱼”还是“鱼”。到目前为止,这是我的代码:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
#include <locale>
using namespace std;
void
usage(char *progname, string msg){
cerr << "Error: " << msg << endl;
cerr << "Usage is: " << progname << " [filename]" << endl;
cerr << " specifying filename reads from that file; no filename reads standard input" << endl;
}
int capitalization(string word){
for(int i = 0; i <= word.length(); i++){
}
}
int main(int argc, char *argv[]){
string adj;
string file;
string line;
string articles[14] = {"a","A","an","aN","An","AN","the","The","tHe","thE","THe","tHE","ThE","THE"};
ifstream rfile;
cin >> adj;
cin >> file;
rfile.open(file.c_str());
if(rfile.fail()){
cerr << "Error while attempting to open the file." << endl;
return 0;
}
string::size_type pos;
string word;
string words[1024];
while(getline(rfile,line,'\n')){
istringstream iss(line);
for(int i = 0; i <= line.length(); i++){
iss >> word;
words[i] = word;
for(int j = 0; j <= 14; j++){
if(word == articles[j]){
string article = word;
iss >> word;
pos = line.find(article);
cout << pos << endl;
capitalization(word);
}
}
}
}
}
我曾尝试用if语句和isupper / islower来弄清楚大写字母,但是很快我发现那是行不通的。谢谢你的帮助。
最佳答案
isupper / islower函数采用单个字符。您应该能够遍历字符串中的字符并检查大小写,如下所示:
for (int i = 0; i < word.length(); i++) {
if (isupper(word[i])) cout << word[i] << " is an uppercase letter!" << endl;
else if (islower(word[i])) cout << word[i] << " is a lowercase letter!" << endl;
else cout << word[i] << " is not a letter!" << endl;
}
当然,在每种情况下,您都可以将cout语句替换为要执行的操作。
关于c++ - 错误:没有匹配函数可调用'isupper(std::string&)'|,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28511048/