Diferentes métodos para invertir una string en C++ – Part 1

La inversión de una string no es más que simplemente sustituir el último elemento de una string en la primera posición de la string.

string-reverse

CPP

// C++ program to reverse a string 
// using first to last approach
// 'for' loop
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a string
void reverseStr(string& str)
{
    int n = str.length();
  
    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
  
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}

C++

// C++ program to reverse a string 
// using while loop
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a string
void reverseStr(string& str)
{
    int len = str.length();
    int n = len-1;
    int i = 0;
    while(i<=n){
        //Using the swap method to switch values at each index
        swap(str[i],str[n]);
        n = n-1;
        i = i+1;
  }
  
}
  
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}

C++

// C++ program to demonstrate reverse
// of a string using Last to First
// Approach 'for' Loop
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a string
void reverse(string str)
{
    for (int i = str.length() - 1; i >= 0; i--)
        cout << str[i];
}
  
// Driver code
int main(void)
{
    string s = "GeeksforGeeks";
    reverse(s);
    return (0);
}

C++

// C++ program to demonstrate reverse
// of a string using Last to First
// Approach 'while' Loop
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a string
void reverse(string str)
{
    int len = str.length();
    int n = len; 
    while(n--)
        cout << str[n];
}
  
// Driver code
int main(void)
{
    string s = "GeeksforGeeks";
  
    reverse(s);
    return (0);
}

C++

// C++ program to reverse a string 
// using recursion
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a string
void reverseStr(string& str, int n, int i)
{
      
  if(n<=i){return;}
//  Swaping the character 
  swap(str[i],str[n]);
  reverseStr(str,n-1,i+1);
  
}
  
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str, str.length()-1, 0);
    cout << str;
    return 0;
}

C++

//C++ program to reverse a string using recursion
#include <iostream>
using namespace std;
void getreverse(string &str, int i)
{
    if (i > (str.length() - 1 - i))
    {
        return;
    }
    swap(str[i], str[str.length() - i - 1]);
    i++;
    getreverse(str, i);
}
int main()
{
    string name = "geeksforgeeks";
  
    getreverse(name, 0);
    cout << name << endl;
    return 0;
}
//code contributed by pragatikohli

CPP

// C++ program to illustrate the
// reversing of a string using 
// reverse() function
#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;
}

CPP

// 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;
}

CPP

// C++ program to demonstrate
// reversing of string 
// using temporary string
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    string str = "GeeksforGeeks";
    int n = str.length();
    
    // Temporary string to store the reverse
    string rev;
    
    for (int i = n - 1; i >= 0; i--)
        rev.push_back(str[i]);
  
    cout << rev << endl;
    return 0;
}

C++

// C++ program to get reverse of a const string
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse string and return
// reverse string pointer of that
char* reverseConstString(char const* str)
{
    // find length of string
    int n = strlen(str);
  
    // create a dynamic pointer char array
    char* rev = new char[n + 1];
  
    // copy of string to ptr array
    strcpy(rev, str);
  
    // Swap character starting from two
    // corners
    for (int i = 0, j = n - 1; i < j; i++, j--)
        swap(rev[i], rev[j]);
  
    // return pointer of the reversed string
    return rev;
}
  
// Driver code
int main(void)
{
    const char* s = "GeeksforGeeks";
    printf("%s", reverseConstString(s));
    return (0);
}

C++

// C++ Program to reverse a string
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s = "GeeksforGeeks";
    stack<char> st;
    for (char x : s)
        st.push(x);
    while (!st.empty()) {
        cout << st.top();
        st.pop();
    }
    return 0;
}

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *