2016-10-30 18 views
0

私は、ユーザ入力メッセージを入力してから、 "参照"を "c"に置き換える必要がある問題に取り組んでいます。私は配列メッセージ[200]を読んで、それを個々の単語に分解したかったのです。私はforループを試しましたが、私がconcatinateするときにそれはただの言葉を追加するだけです。私は、文字列、文字列を使用しないでください。C++文字配列内の単語を置換する

const int MAX_SIZE = 200; 

int main(){ 

    char message[MAX_SIZE]; //message array the user will enter 
    int length;  // count of message lenght 
    int counter, i, j;  //counters for loops 
    char updateMessage[MAX_SIZE]; //message after txt update 

    //prompt user to 
    cout << "Please type a sentence" << endl; 
    cin.get(message, MAX_SIZE, '\n'); 
    cin.ignore(100, '\n'); 

    length = strlen(message); 
    //Lower all characters 
    for(i = 0; i < length; ++i) 
    { 
     message[i] = tolower(message[i]); 


    //echo back sentence 
    cout << "You typed: " << message << endl; 
    cout << "Your message length is " << length << endl; 

    for(counter = 0; counter <= length; ++counter) 
    { 

      updateMessage[counter] = message[counter]; 

      if(isspace(message[counter]) || message[counter] == '\0') 
      { 
        cout << "Space Found" << endl; 
        cout << updateMessage << endl; 
        cout << updateMessage << " ** " << endl; 

      } 
    } 
return 0; 
} 

それぞれのスペースが見つかったら、それぞれ1つずつしか出力しません。

+1

「私は出力のみ1つのワークそれぞれたい」という質問ではありません。 –

+0

あなたは正しいです。ごめんなさい。どのように連言なしで各単語を出力するのですか? –

+0

このプログラムの唯一の "C++"はcoutです。これは実際にはCコードです。 – dmg

答えて

1

現代のC++や標準ライブラリの機能を実際に習得する必要があるので、C++でCコードを書くことにはなりません。例として、これはC++ 14のプログラムは、コードの10〜15行で仕事をするためのライブラリからの標準アルゴリズムを使用する方法です:

#include <algorithm> 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 

int main() 
{ 
    using namespace std::string_literals; 

    std::istringstream input("Hello I see you, now you see me"); 
    std::string str; 

    // get the input from the stream (use std::cin if you read from console) 
    std::getline(input, str); 

    // tokenize 
    std::vector<std::string> words; 
    std::istringstream ss(str); 
    for(std::string word ; ss >> word; words.push_back(word)); 

    // replace 
    std::replace(words.begin(), words.end(), "see"s, "c"s); 

    // flatten back to a string from the tokens 
    str.clear(); 
    for(auto& elem: words) 
    { 
     str += elem + ' '; 
    } 

    // display the final string 
    std::cout << str; 
} 

Live on Coliru

これは、ほとんどありませんその代わりにコードを実行し、CPUサイクルのすべてのビットを節約する必要がなければ、正常に動作します。

以下

std::vectorを回避し、代わりに交換を行い、ソリューションです:

#include <algorithm> 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 

int main() 
{ 
    std::istringstream input("Hello I see you, now you see me"); 
    std::string str; 

    // get the input from the stream (use std::cin if you read from console) 
    std::getline(input, str); 

    // tokenize and replace in place 
    std::istringstream ss(str); 
    std::string word; 
    str.clear(); 
    while (ss >> word) 
    { 
     if (word == "see") 
      str += std::string("c") + ' '; 
     else 
      str += word + ' '; 
    } 

    // display the final string 
    std::cout << str; 
} 

Live on Coliru

関連する問題