Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,978 questions

51,922 answers

573 users

How to print the middle words of a string reversed with C++

1 Answer

0 votes
#include <iostream> 
#include<algorithm> 

using namespace std; 
   
void print_reverse_middle_words(string s) { 
    int i = 0; 
    for (i = 0; i < s.length() && s[i] != ' '; i++) 
        cout << s[i]; 
   
    string word = ""; 
    for (; i < s.length(); i++) { 
         if (s[i] != ' ') 
            word += s[i]; 
         else { 
            reverse(word.begin(), word.end()); 
            cout << word << " "; 
            word = ""; 
         } 
    } 
    cout << word; 
} 
   
int main() 
{ 
    string s = "c++ java python vb.net"; 
     
    print_reverse_middle_words(s); 
     
    return 0; 
} 
 
 
 
/*
run:
 
c++ avaj nohtyp vb.net
 
*/

 



answered Dec 5, 2019 by avibootz

Related questions

2 answers 660 views
2 answers 142 views
142 views asked Oct 9, 2022 by avibootz
1 answer 80 views
2 answers 90 views
1 answer 136 views
1 answer 166 views
...