ここに私の完全な解決策があります。処理する単語のソースとしてコマンドライン引数を使用します。このコードは、word内の各位置を調べて、Rule1またはRule2が適用されているかどうかを確認します。単語内の特定のインデックスにルールが適用されると、ルールはそのインデックスに適用され、ループは残りの単語の検索を続け、一致するルールをさらに探します。
すべての単語が処理された後、ハイフネーションされている場合とされていない場合がある処理済みの単語が出力されます。各コマンドライン引数は1行に出力されます。ここで
は、いくつかのサンプル出力です。(私のbash promotは ">" 文字である)ここで
> hyphenate
usage: hyphenate <string to hyphenate>
> hyphenate "HELLO World! You Rule!" Test Evil One
HEL-LO World! You Ru-le!
Test
E-vil
One
は、コードは次のとおりです。
#include <iostream> // std::cout
#include <sstream> // std::istringstream
#include <string> // std::string
#include <cctype> // std::tolower
using namespace std;
void DisplayUsage(int argc, char** argv) {
cout << "usage: hyphenate <string to hyphenate>" << endl;
}
bool IsaVowel(char c) {
bool Vowel = false;
// Simplify switch by using tolower to convert uppercase to lowercase
// Only 5 cases to consider
switch(tolower(c)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Vowel = true;
}
return Vowel;
}
bool IsaConsonant(char c) {
// A character is a consonant if it is alphabetic and not a vowel
return isalpha(c) && !IsaVowel(c);
}
bool VCCV(string s, int idx) {
bool matchFound = false;
// Only check for the pattern if there are enough characters past idx
if (idx+3 < s.length())
// return true only if see a VCCV pattern
if (IsaVowel(s.at(idx))
&& IsaConsonant(s.at(idx+1))
&& IsaConsonant(s.at(idx+2))
&& IsaVowel (s.at(idx+3)))
matchFound = true;
return matchFound;
}
bool VCV(string s, int idx) {
bool matchFound = false;
// Only check for the pattern if there are enough characters past idx
if (idx+2 < s.length())
// return true only if see a VCV pattern
if (IsaVowel(s.at(idx))
&& IsaConsonant(s.at(idx+1))
&& IsaVowel (s.at(idx+2)))
matchFound = true;
return matchFound;
}
bool VCe(string s, int idx) {
bool matchFound = false;
// VCe matches if VCV matches, and idx+2 is the last letter, and its an e
if (VCV(s,idx) && (idx+2==(s.length()-1)) && (s.compare(idx+2,1,"e") == 0))
matchFound = true;
return matchFound;
}
bool Rule1(string s, int idx) {
return VCCV(s,idx);
}
bool Rule2(string s, int idx) {
return VCV(s,idx) && !VCe(s,idx);
}
// This program assumes compiled using the -std=c++11 standard
int main(int argc, char** argv) {
if (argc < 2) {
// Did not provide a string to hyphenate, display usage and exit
DisplayUsage(argc, argv);
return -1; // Error
}
// Process each of the supplied parameters which may each contains multiple words
for(int i=1; i<argc; i++) {
// Get the next command line parameter
istringstream newWords(argv[i]);
while (!newWords.eof()) {
string nextWord;
newWords >> nextWord;
// Scan the next word to see if any rules apply
for(int j=0; j<nextWord.length(); j++) {
if (Rule1(nextWord, j))
nextWord.insert(j+2,"-");
if (Rule2(nextWord, j))
nextWord.insert(j+1,"-");
}
cout << nextWord << " ";
}
cout << endl;
}
return 0;
}
ファイルハイフネーションに保存し、これをコンパイルします。 cppを実行し、-std = C++ 11オプションを指定してコンパイルします。 ?// EN:どのように私は `VCCV`になるためにVCCV``わたってるしきハイフンを追加することができます_あなたも[ `のstd :: STRING`]のドキュメントを見てみてくださいました(HTTP _but
g++ -o hyphenate -std=c++11 hyphenate.cpp
.cppreference.com/w/cpp/string/basic_string)。文字を挿入するために使用できる['insert'](http://en.cppreference.com/w/cpp/string/basic_string/insert)メソッドがあります。 –
だから私は、適切な条件を指定してif文を使用し、それが通過すれば挿入しますか? – laura
"必ずネストされたif文を使う必要があると思います。 'if(a){if(b){std :: cout <<" AとB "; }} 'は' if(a && b){std :: cout << "AとB"として書くこともできます。 } '。実際には、人間にとって最も読みやすいものを選択します。コンピュータは気にしません。 – MSalters