任何人都可以向我解释他们如何找出字符串字母的大小写和小写字母?我需要知道单词是否是" fish"," Fish"," FISH"或者" fISH。"到目前为止,这是我的代码:
#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搞清楚大写,但我很快发现那不会起作用。谢谢你的帮助。
答案 0 :(得分:1)
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语句。