Rita Picasso
0
Q:

string reverse iterator c++

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

using namespace std;

int main() {
	string s = "live";
  	reverse(s.begin(), s.end());
  	cout << s; //evil
  	return 0;
}
12
// using reverse() 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    string str = "geeksforgeeks"; 
    reverse(str.begin(), str.end()); 
} 
17
// C++ program to reverse individual words in a given  
// string using STL list 
#include <bits/stdc++.h> 
using namespace std; 
  
// reverses individual words of a string 
void reverseWords(string str) 
{ 
    stack<char> st; 
  
    // Traverse given string and push all characters 
    // to stack until we see a space. 
    for (int i = 0; i < str.length(); ++i) { 
        if (str[i] != ' ') 
            st.push(str[i]); 
  
        // When we see a space, we print contents 
        // of stack. 
        else { 
            while (st.empty() == false) { 
                cout << st.top(); 
                st.pop(); 
            } 
            cout << " "; 
        } 
    } 
  
    // Since there may not be space after 
    // last word. 
    while (st.empty() == false) { 
        cout << st.top(); 
        st.pop(); 
    } 
} 
  
// Driver program to test function 
int main() 
{ 
    string str = "Geeks for Geeks"; 
    reverseWords(str); 
    return 0; 
} 
-1
// Using iterators
for (auto it = s.crbegin() ; it != s.crend(); ++it) {
  std::cout << *it;
}

// Naive
for (int i = s.size() - 1; i >= 0; i--) {
  std::cout << s[i];
}
0
// string::rbegin/rend
#include <iostream>
#include <string>

int main ()
{
  std::string str ("now step live...");
  for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
    std::cout << *rit;
  return 0;
}
0
...evil pets won
0

New to Communities?

Join the community