有人能向我解释一下他们将如何找出字符串单词的大写字母和小写字母吗?我需要知道这个词是说"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来计算大写,但我很快发现这行不通。谢谢你的帮助。
isupper/ispower函数接受单个字符。您应该能够循环浏览字符串中的字符,并检查大小写,如下所示:
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语句。