My input is "Hello World" and my target result is "olleH dlroW".
So, my idea is to get the sentence into a variable, and then iterate over the words in the sentence, cancel each of them, and finally merge them into a new variable.
My question is: how to iterate over sentence words?
#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; }
Before you can iterate over words in a sentence, you need to read the sentence from the input. This line
cin >> sentence;
reads the first word of a sentence, not the whole sentence. Use instead getline:
getline
std::getline(std::cin, sentence);
With sentencein memory, you can iterate over it sequentially using istream_iteratoras follows:
sentence
istream_iterator
stringstream ss(sentence); for (auto w = istream_iterator<string>(ss) ; w != istream_iterator<string>() ; w++) { string &word = *w; ... }
Demo version
for(short i=0;i<sentence.length();i++){ if(sentence[i] == ' '){ counter++; i++; } words[counter] += sentence[i]; }
, , words[]
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; }
. , ...!!
NB: cin , . std::getline(std::cin, sentence) .
std::getline(std::cin, sentence)
std::reverse()
, find reverse :
find
reverse
#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; }
Input:
this is a nice day
:
this is a nice day siht si a ecin yad
, ? () ,
, , , .. cin >> sentence ( ).
cin >> sentence
, "". - - - . , , , . , "", , split (cin >> word)
split
cin >> word
, , (, ) . Regex word (, "\ w +" ).
"" , . , ( split, Regex - ) .
, "" , .
Boost boost::split:
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; }
- .
#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"; } }
This suggests that the word "is defined" as "a sequence of characters without spaces". It is easy to substitute a different class of characters instead of "non-spaces", for example. for alphanumeric characters use std::isalnum. A definition that reflects the real concept of a word, for example. used in the natural language sciences goes far beyond the scope of this answer.
std::isalnum
Source: https://habr.com/ru/post/1687508/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1687503/passing-a-javascript-array-of-strings-to-a-c-function-with-emscripten&usg=ALkJrhhru2tCRek58s1sTlRwj_QkpQEn3winnerHTML will not change in every keydown event - javascriptCan I get the generated identifier for a document created using batch (). Set using Firestore? - javascriptCursors change on iOS - mobile-safariReact Native - logout user if api status code is 401 (unauthorized) - react-nativeHow to convert fields during deserialization using Serde? - rustFirebase - Firestore - how many times will I read documents - observableCreating a bookmarklet using webpack, bookmarklet-loader, style and css-loader - javascriptLay one iframe on top of the other, scroll them together - htmlhtaccess if the url contains a redirect line to the page without changing the url - redirectAll Articles