2017-10-15 62 views
4

我的輸入是「Hello World」,我的目標輸出是「olleH dlroW」。如何迭代C++中的一個句子的單詞?

所以我的想法是把句子變成一個變量,然後遍歷句子中的單詞,顛倒它們中的每一個,最後將它們連接成一個新的變量。

我的問題是:如何迭代句子的單詞?

#include <iostream> 
#include <stdio.h> 
#include <string.h> 
using namespace std; 

string reverseword(string word) 
{ 
    string rword; 
    int size = word.length(); 
    while (size >= 0) 
    { 
     rword+= word[size]; 
     size = size -1; 
    } 
    return rword; 
} 

int main() 
{ 
    string sentence; 
    cout<<"Enter the word/sentence to be reversed: "; 
    cin >> sentence; 
    string rsentence; 
    // for every word in the sentence do 
    { 
     rword = reverseword(word); 
     rsentence = rsentence + " " + rword; 
    } 
    cout<<rword; 
    return 0; 
} 
+3

僅供參考'std :: reverse'做你的'反向字詞'功能:P – Rakete1111

+0

你可以運行一個toop直到你找到空格並抓住所有字符並把它們放到一個變量中並且做相反 –

+0

見https:/ /stackoverflow.com/questions/236129/most-elegant-way-to-split-a-string拆分 – fundagain

回答

6

在迭代句子中的單詞之前,需要從輸入中讀取一個句子。此行

cin >> sentence; 

讀取一個句子的第一個單詞,而不是整個句子。使用getline來代替:

std::getline(std::cin, sentence); 

隨着內存sentence,您可以按如下迭代它字的字使用istream_iterator

stringstream ss(sentence); 
for (auto w = istream_iterator<string>(ss) ; w != istream_iterator<string>() ; w++) { 
    string &word = *w; 
    ... 
} 

Demo.

+0

根據輸入,你不需要stringstream,但可以直接使用'std :: cin'。 – Rakete1111

+0

@ Rakete1111對,我只是不想假設OP不會從用戶那裏得到任何額外的輸入,所以我添加了一個'stringstream'。 – dasblinkenlight

+0

很好的解決方案。你如何處理空格,逗號等? – Chiel

1
for(short i=0;i<sentence.length();i++){ 

     if(sentence[i] == ' '){ 
      counter++; 
      i++; 
     } 

     words[counter] += sentence[i]; 
    } 

注意上述循環用空間分割句子,並將其存儲到一個字符串數組,words[]

#include <iostream> 
#include <stdio.h> 
#include <string.h> 

using namespace std; 

string reverseword(string word) // function to reverse a word 
{ 
    string rword; 
    int size = word.length(); 
    while (size >= 0) 
    { 
     rword+= word[size]; 
     size = size -1; 
    } 
    return rword; 
} 

int main() 
{ 
    string sentence; 

    cout << "Enter the word/sentence to be reversed: "; 
    std::getline(std::cin, sentence); 


    string rsentence; 
    string words[100]; 


    string rword; 

    short counter = 0; 

    for(short i=0; i<sentence.length(); i++){ // looping till ' ' and adding each word to string array words 

     if(sentence[i] == ' '){ 
      counter++; 
      i++; 
     } 

     words[counter] += sentence[i]; 
    } 



    for(int i = 0; i <= counter; i++) // calling reverse function for each words 
    { 
     rword = reverseword(words[i]); 

     rsentence = rsentence + " " + rword; // concatenating reversed words 
    } 

    cout << rsentence; // show reversed word 

    return 0; 
} 

我已校正的代碼。希望這可以幫助...!!

注意:您正在使用cin讀取不可能的空格分隔字符串。您必須使用std::getline(std::cin, sentence)來讀取空格分隔的字符串。

您還可以使用std::reverse()扭轉字符串

+0

請使用'std :: vector '而不是堆棧分配數組。 – Chiel

+0

請解釋downvote –

+0

我想它與我的評論有關。 C++有容器,可以避免聲明像'words [100]'這樣的數組。這是不安全的,可能導致分段錯誤。我建議你編輯它,因爲你的答案有一些很好的元素。 – Chiel

0

的回答上面給出了一個辦法你輸入轉換爲詞,即cin >> sentence返回一個「字」(如此,只是把它反覆)。

但是,這引出了什麼是「單詞」的問題。您想將計算機構造 - 字符串 - 翻譯成更復雜的形式 - 單詞。所以,你必須明確你想要的單詞時的意思。它可以是「空」分隔子或您的字符串作爲簡單 - 然後用split功能,或者在一個時間(cin >> word

讀你的串詞或者你可能有更嚴格的要求,就像他們不能包括標點符號(如句末)或數字。然後考慮使用正則表達式和單詞模式(如「\ w +」)。

或者你可能想要像你會在字典中找到的「真實」的單詞。然後,您需要考慮您的語言環境,將輸入解析爲塊(使用拆分,正則表達式或其他語言),然後在人類語言詞典中查找每個塊。

換句話說,「單詞」解析只是像您的要求那樣簡單或複雜。

+0

這是一個很棒的評論,但它沒有回答這個問題。 –

+0

它建議使用'cin','split','regex'和字典查找。所以它確實回答了這個問題,它只是沒有提供OP的程序。 – Les

0

有增強您可以使用boost::split功能:

#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <boost/algorithm/string.hpp> 

int main() 
{ 
    std::string sentence = "Hello world"; 

    std::vector<std::string> words; 
    boost::split(words, sentence, boost::is_any_of(" ")); 

    std::string rsentence; 
    for (std::string word : words) // Iterate by value to keep the original data. 
    { 
     std::reverse(word.begin(), word.end()); 
     rsentence += word + " "; // Add the separator again. 
    } 
    boost::trim(rsentence); // Remove the last space. 

    std::cout << rsentence << std::endl; 

    return 0; 
} 
1

下面是一個使用findreverse實現了輸出的解決方案:

#include <iostream> 
#include <string> 
#include <algorithm> 


int main() { 
    std::string sentence; 
    std::getline(std::cin, sentence); 
    std::cout << sentence << std::endl; 
    size_t cpos = 0; 
    size_t npos = 0; 
    while((npos = sentence.find(' ', cpos)) != std::string::npos) 
    { 
     std::reverse(sentence.begin() + cpos, sentence.begin() + npos); 
     cpos = npos + 1; 
    } 
    std::reverse(sentence.begin() + cpos, sentence.end()); 
    std::cout << sentence << std::endl; 
    return 0; 
} 

輸入:

this is a nice day 

輸出:

this is a nice day 
siht si a ecin yad 
-1

這個答案是我對抗全球變暖的卑微貢獻。

#include <string>                
#include <iostream>               
#include <algorithm>               
#include <cctype>                

int main()                 
{                    
    std::string sentence;              
    while (std::getline(std::cin, sentence))         
    {                   
     auto ws = sentence.begin();           

     while (ws != sentence.end())           
     {                  
      while (std::isspace(*ws)) ++ws;         
      auto we = ws;              
      while (we != sentence.end() && !std::isspace(*we)) ++we;   
      std::reverse(ws, we);            
      ws = we;               
     }                  
     std::cout << sentence << "\n";          
    }                   
} 

這裏假定「單詞」被定義爲「非空白字符序列」。用一個不同的字符類代替「非空白」是很容易的,例如,對於字母數字字符,請使用std::isalnum。反映現實世界概念的定義例如在自然語言科學中使用遠遠超出了這個答案的範圍。

相關問題