fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int vowel_counter(string word){
  5. int n_vowels = 0;
  6. string vowels = "aeiou";
  7. for(char c: word){
  8. if(vowels.find(c) != string::npos){
  9. n_vowels++;
  10. }
  11. }
  12.  
  13. return n_vowels;
  14. }
  15.  
  16. void flippedy(string sentence){
  17. int n_vowels;
  18. int i = 0;
  19. string result = "";
  20.  
  21. stringstream ss(sentence);
  22.  
  23.  
  24. for (auto w = istream_iterator<string>(ss);
  25. w != istream_iterator<string>(); w++ ){
  26.  
  27. string word = *w;
  28.  
  29. // count n_vowels from the first word
  30. if(i == 0){
  31. n_vowels = vowel_counter(word);
  32. }
  33.  
  34. // logic for non-first word
  35. else{
  36. if(n_vowels == vowel_counter(word)){
  37. reverse(word.begin(), word.end());
  38. }
  39. result.append(" ");
  40. }
  41.  
  42. result.append(word);
  43. i++;
  44. }
  45.  
  46.  
  47. cout << "Final Result: " << result;
  48.  
  49. }
  50.  
  51. int main(){
  52.  
  53. string sentence;
  54.  
  55. cout << "Enter the sentence: ";
  56. getline(cin, sentence);
  57.  
  58. flippedy(sentence);
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5328KB
stdin
cat and mice
stdout
Enter the sentence: Final Result: cat dna mice