KeepCoding
0
Q:

reverse string in c++ without using function

#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
#include <iostream>
using namespace std;
int main()
{
    char str[] = "Reverseme";
    char reverse[50];
    int i=-1;
    int j=0;
         /*Count the length, until it each at the end of string.*/ 
          while(str[++i]!='\0');
           while(i>=0)
                    reverse[j++]=str[--i];
            reverse[j]='\0';
      cout<<"Reverse of  a string is"<< reverse;
      return 0;
}
1
// A quickly written program for reversing a string 
// using reverse() 
#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
    string str = "geeksforgeeks"; 
  
    // Reverse str[begin..end] 
    reverse(str.begin(), str.end()); 
  
    cout << str; 
    return 0; 
} 
1
// A simple C++ program to reverse string using constructor  
#include <bits/stdc++.h>  
using namespace std;  
int main(){ 
  
    string str = "GeeksforGeeks"; 
  
    //Use of reverse iterators 
    string rev = string(str.rbegin(),str.rend()); 
  
    cout<<rev<<endl;  
    return 0; 
} 
1
// 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

New to Communities?

Join the community