fork download
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. void RevWords(string inp);
  6.  
  7.  
  8. int main()
  9. {
  10. string input;
  11.  
  12. cout<<"Enter Sring:"<<endl;
  13. getline(cin,input);
  14. cout<<"Entered String:"<<input<<endl;
  15. RevWords(input);
  16.  
  17. return 0;
  18. }
  19.  
  20. void RevWords(string inp)
  21. {
  22. int wordEnd=0,indexS=0,indexE=0;
  23. string newStr;
  24. newStr=inp;
  25.  
  26. while(wordEnd<=inp.length())
  27. {
  28. if(wordEnd < inp.length() && inp[wordEnd] != ' ')
  29. {
  30. wordEnd++;
  31. }
  32. else
  33. {
  34. if(inp[wordEnd] == ' ' || inp[wordEnd] == '\0')
  35. {
  36. indexE=wordEnd-1;
  37. while(indexS<wordEnd)
  38. {
  39. newStr[indexS]=inp[indexE];
  40. indexS++;
  41. indexE--;
  42. }
  43. newStr[indexS]=' ';
  44. indexS++;
  45. }
  46. wordEnd++;
  47. }
  48. }
  49. cout<<newStr<<endl;
  50. }
Success #stdin #stdout 0s 3468KB
stdin
words are fleeting
stdout
Enter Sring:
Entered String:words are fleeting
sdrow era gniteelf