2016-04-25 21 views
-1

私は段落の単語の数と各単語のインデックスを見つける必要があるプロジェクトに取り組んでいます...私は文字列内の単語の数を数えているコードを書いていますが、言葉のインデックス、このような段落の単語のシーケンス番号(インデックス)を見つける方法はありますか?

など:こんにちはジョン、あなたが私はあなたを欠場どのように..です

私のようなインデックス印刷する必要があります:0 1 2 3 4 5 6 7

を、ここでのコードは次のとおりです。

+0

私はあなたの質問を理解しているか分からない。 'n '個の単語を数えると、インデックスは' 0からn-1'になります。それとも違うの? –

+0

はい、まさにその意味です@bkVnet –

+0

カウントがあれば、 '0からn-1'までそれらの数字を表示するのはどうですか? –

答えて

0

このヘルプs。 count_words関数の使用を含むように編集されました。

#include <iostream> 
#include <sstream> 

void count_words(std::string); 

int main(){ 
    std::string input_text, output_text; 
    std::cout<< "Enter a text: "; 
    std::getline(std::cin,input_text); 

    count_words(input_text); 


    system ("PAUSE"); 

    return 0; //MUST RETURN AN INTEGER VALUE FROM 'INT MAIN' 
} 

void count_words(std::string inputString){ 
    std::string output_text; 
    std::stringstream indexes; 
    int number_of_words=0; //If there are no words, it would be false, make it 0. 
    //int counter []={0}; //This serves no purpose. 
    if(!inputString.empty()){// test to make sure it isn't empty. 
     number_of_words++; 
     for(int i = 0; i < inputString.length();i++){ // For loops should have curly braces {} containing their statement. 
      if(inputString[i] == ' '){ 
       number_of_words++; 
      } 
      if((isalpha(inputString[i]))&&inputString[i-1]==' '){ //test for following space separated word 
       indexes << i << " "; 
      } 

     } 
    } 

    output_text = indexes.str(); //convert stringstream to string 

    std::cout << "Number of words: " << number_of_words << std::endl; 
    //std:: cout << number_of_words << std::endl; //duplicate info 
    std::cout << "Indexes: " << output_text << std::endl; 
} 
+0

@ NonCreature0714とても感謝します。しかし、私はstd :: stringstreamインデックスにエラーがあります。 .....(不完全なタイプは許可されていません)、 アイデアはありますか? –

+0

ああ、# NonCreature0714

0

私は質問が分かりますか分かりません。あなたは単に "インデックス"を印刷する必要がありますか?このような? (自分のコードを使用して)

#include <iostream> 
#include <vector> 
#include <string> 

void stringTokenizer(const std::string& str, const std::string& delimiter, std::vector<std::string>& tokens) { 
    size_t prev = 0, next = 0, len; 

    while ((next = str.find(delimiter, prev)) != std::string::npos) { 
    len = next - prev; 
    if (len > 0) { 
     tokens.push_back(str.substr(prev, len)); 
    } 
     prev = next + delimiter.size(); 
    } 

    if (prev < str.size()) { 
    tokens.push_back(str.substr(prev)); 
    } 
} 

int main() 
{ 

    std::vector <std::string> split; 
    std::string input_text; 
    std::cout<< "Enter a text: "; 
    std::getline(std::cin,input_text); 

    stringTokenizer(input_text, " ", split); 
    int number_of_words = 0; 
    for (std::vector<std::string>::iterator it = split.begin(); it != split.end(); it++, number_of_words++) { 
    std::cout << *it << " " << number_of_words << std::endl; 
    } 

} 
+0

お礼ありがとうございました。それはうまくいきました。私は別の質問をお願いします。 どのように各文章をその索引で印刷できますか? ありがとうございます! –

+0

これについて考えてみましょう:どのくらいの単語がありますか? ""こんにちは。 – user4581301

+0

私はあなたに助けてくれることを願っています。あなたの宿題を学ぶだけでなく、 – YosefMac

関連する問題