字符串反转程序(迭代和递归)


reverse() 是一个预安装的预定义头文件,在 C++ 环境中的过程中用作模板。该方法能够以从后到前的顺序反转任何值容器中的元素。此过程的时间复杂度为 O(n)。假设我们声明了一个包含一些数据元素的字符串 str[],现在任务是对该字符串执行反转操作以获得最终结果。

这是一个过程的一般示例:

Input string is here : S = "ARBRDD"
Output After The Process : S = "DDRBRA"
Input string is here : S = "Right Now I Am Here In Kolkata!"
Output After The Process : S = "!atakloK nI ereH mA I woN thgiR"

对特定字符串执行反转过程的算法

在这个可能的算法中,我们将使用 C++ 环境对特定字符串执行反转过程。使用此算法,我们将构建一些 C++ 语法,以便以有效的方式学习问题陈述。

  • 步骤 1 - 开始过程。

  • 步骤 2 - 声明输入输出流。

  • 步骤 3 - 导入内置类和声明的函数。

  • 步骤 4 - 构造和声明一个字符串。

  • 步骤 5 - 使用一些值填充字符串。

  • 步骤 6 - 调用递归或迭代函数来反转字符串。

  • 步骤 7 - 如果声明的字符串为空,则打印整个字符串。

  • 步骤 8 - 或者,如果只有一个值,则再次打印整个字符串。

  • 步骤 9 - 否则,如果有多个字符值;调用递归函数并执行字符串反转过程。

  • 步骤 10 - 将值打印为反转字符串。

  • 步骤 11 - 终止过程。

对特定字符串执行反转过程的语法

char str1[100], *ptr;
   int len1 = 0, i;
   char ch;
   printf("Enter the string to apply the process:\n");
   scanf("%[^\n]s", str1);
   ptr = str1;
   len1 = strlen(str1);
   printf("Using the process of iteration:\n");
   for (i = len1 - 1; i >= 0;i--){
      ch = str1[i];
      printf("%c", ch);
  }
   printf("Using the process of recurssion:\n");
   disp_str1_rec(ptr);
}
void disp_str1_rec(char *stng){
   char ch;
   if (*stng != '\0'){
      ch = *stng;
      stng++;
      disp_str1_rec(stng);
      printf("%c", ch);
   }
   else
   return;

在这个可能的语法中,我们将使用 C++ 环境对特定字符串执行反转过程。使用这些特定的语法,我们将学习一些 C++ 程序,以全面了解问题陈述。

遵循的方法

  • 方法 1 - 使用栈、迭代、双指针、reverse()、std::reverse() 和 C++ STL 方法的 C++ 字符串反转程序。

  • 方法 2 - 使用递归、递归栈、斐波那契数列、引用参数、引用参数方法和双向链表的 C++ 字符串反转程序。

方法 1:使用栈、迭代、双指针、Reverse()、Std::reverse() 和 C++ STL 方法

栈方法的使用

在这种可能的方法中,我们将构建并应用一个栈来从主字符串打印反转字符串。

void reverse(string& str){
   reverse(str.begin(), str.end());
}
void reverse(char *str){
   reverse(str, str + strlen(str));
}
int main(){
   string str = "ENTER THE INPUT STRING HERE";
   reverse(str);
   cout << "Reverse of the given std::string is here after the process " <<
str << endl;
   char s[] = "ENTER THE INPUT STRING HERE";
   reverse(s);
   cout << "Reverse of the given C-string is " << s;

示例

//C++ program to reverse a string using stack
#include <bits/stdc++.h>
using namespace std;
void recursiveReverse(string &str){
   stack<char> st;
   for (int i=0; i<str.length(); i++)
   st.push(str[i]);
   for (int i=0; i<str.length(); i++) {
      str[i] = st.top();
      st.pop();
   }
}
int main(){
   string str = "NABDNIAKHDLOKDDRBRA";
   recursiveReverse(str);
   cout << str;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

迭代方法的使用

在这种可能的方法中,我们将应用迭代方法从主字符串打印反转字符串。

示例

//C++ program to reverse a string by using iteration
#include <bits/stdc++.h>
using namespace std;
void reverseStr(string& str){
   int n = str.length();
   for (int i = 0; i < n / 2; i++)
   swap(str[i], str[n - i - 1]);
}
int main(){
   string str = "NABDNIAKHDLOKDDRBRA";
   reverseStr(str);
   cout << str;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

使用两个指针的迭代方法

在这种可能的方法中,我们将使用两个指针应用迭代方法从主字符串打印反转字符串。

示例

//C++ program to reverse a string by using iteration with two pointers
#include <bits/stdc++.h>
using namespace std;
void reverseStr(string& str){
   int n = str.length();
   for (int i=0, j=n-1; i<j; i++,j--)
   swap(str[i], str[j]);
}
int main(){
   string str = "NABDNIAKHDLOKDDRBRA";
   reverseStr(str);
   cout << str;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

Reverse() 方法的使用

在这种可能的方法中,我们应用了 reverse() 方法函数从主字符串打印反转字符串。

示例

//C++ program to reverse a string by using the reverse() method
#include<bits/stdc++.h>
using namespace std;
int main(){
   string str = "NABDNIAKHDLOKDDRBRA";
   reverse(str.begin(),str.end());
   cout << str;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

Std:reverse() 方法的使用

在这种可能的方法中,我们将应用 std::reverse() 方法从主字符串打印反转字符串。

示例

//C++ program to reverse a string by using the std::reverse() method
#include <algorithm>
#include <iostream>
#include <string>
int main(){
   std::string str = "NABDNIAKHDLOKDDRBRA";
   std::reverse(str.begin(),str.end());
   std::cout << str << std::endl;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

在 C++ STL 方法中使用“reverse()”

在这种可能的方法中,我们应用了 C++ STL 方法函数中的内置方法“reverse()”从主字符串打印反转字符串。

示例

//C++ program to reverse a string by using the inbuilt method ‘reverse()’ in C++ STL method
#include <bits/stdc++.h>
using namespace std;
int main(){
   string str = "NABDNIAKHDLOKDDRBRA";
   reverse(str.begin(), str.end());
   cout << str;
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

方法 2:使用递归、递归栈、斐波那契数列、引用参数、引用参数方法和双向链表

递归方法的使用

在这种可能的方法中,我们将应用递归方法从主字符串打印反转字符串。

void reverse(const string& a);
int main(){
   string str;
   cout << " INPUT STATEMENT FOR THE PROCESS" << endl;
   getline(cin, str);
   reverse(str);
   return 0;
}
void reverse(const string& str){
size_t numOfChars = str.size();
if(numOfChars == 1) {
   cout << str << endl;
}
else {
cout << str[numOfChars - 1];
reverse(str.substr(0, numOfChars - 1));

示例

//C++ program to reverse a string by using the process of recursion
#include <bits/stdc++.h>
using namespace std;
void reverse(string str){
   if(str.size() == 0){
      return;
   }
   reverse(str.substr(1));
   cout << str[0];
}
int main(){
   string a = "NABDNIAKHDLOKDDRBRA";
   reverse(a);
   return 0;
}

输出

ARBRDDKOLDHKAINDBAN

递归栈方法的使用

在这种可能的方法中,我们将构建和应用递归栈来应用递归方法。通过这种方法,我们可以从主字符串打印反转字符串。

示例

//C++ program to reverse a string using recursion with a recursive stack
#include <bits/stdc++.h>
using namespace std;
void reverse(char *str, int index, int n){
   if(index == n){
      return;
   }
   char temp = str[index];
   reverse(str, index+1, n);
   cout << temp;
}
int main(){
   char a[] = "NABDNIAKHDLOKDDRBRA";
   int n = sizeof(a) / sizeof(a[0]);
   reverse(a, 0, n);
   return 0;
}

输出

.ARBRDDKOLDHKAINDBAN

斐波那契数列的使用

在这种可能的方法中,我们应用了斐波那契数列从主字符串打印反转字符串。

示例

//C++ program to reverse a string using recursion with a fibonacci series
#include <bits/stdc++.h>
using namespace std;
void fibo(int n, int a, int b){
      if (n > 0){
      fibo(n - 1, b, a + b);
      cout << a << " ";
   }
}
int main(){
   int N = 10;
   fibo(N, 0, 1);
   return 0;
}

输出

34 21 13 8 5 3 2 1 1 0

将字符串用作引用参数

在这种可能的方法中,我们将应用引用参数方法在 C++ 环境中从主字符串打印反转字符串。

示例

//C++ program to reverse a string using recursion where a string acts like a reference parameter
#include <iostream>
#include <algorithm>
using namespace std;
void reverse(string &str, int k){
   static int i = 0;
   if (k == str.length()) {
      return;
   }
   reverse(str, k + 1);
   if (i <= k) {
      swap(str[i++], str[k]);
   }
}
int main(){
   string str = "I AM GOING TO KASHMIR TODAY";
   reverse(str, 0);
   cout << "Reverse of the given string is here. Have a look ->" << str;
   return 0;
}

输出

Reverse of the given string is here. Have a look ->YADOT RIMHSAK OT GNIOG MA I

将字符串用作类似引用参数

在这种可能的方法中,我们将声明一个字符串充当具有交换过程的引用参数。使用此方法,用户将能够从主字符串打印反转字符串。

示例

//C++ program to reverse a string using recursion where a string acts like a reference parameter with a swaping process
#include <iostream>
#include <algorithm>
using namespace std;
void reverse(string &str, int l, int h){
   if (l < h){
      swap(str[l], str[h]);
      reverse(str, l + 1, h - 1);
   }
}
int main(){
   string str = "Right Now I Am Here In Srinagar!";
   reverse(str, 0, str.length() - 1);
   cout << "Reverse of the given string is here. Have A Look ->" << str;
   return 0;
}

输出

Reverse of the given string is here. Have A Look ->!raganirS nI ereH mA I woN thgiR

双向链表的使用

在这种可能的方法中,我们将构造和应用双向链表,以便在 C++ 环境中从声明的主字符串打印反转字符串。

示例

//C++ program to reverse a string using recursion with a doubly linked list
#include <bits/stdc++.h>
using namespace std;
struct Node {
   int data;
   Node *next, *prev;
};
Node* getNode(int data){
   Node* new_node = new Node;
   new_node->data = data;
   new_node->next = new_node->prev = NULL;
   return new_node;
}
void push(Node** head_ref, Node* new_node){
   new_node->prev = NULL;
   new_node->next = (*head_ref);
   if ((*head_ref) != NULL)
   (*head_ref)->prev = new_node;
   (*head_ref) = new_node;
}
Node* Reverse(Node* node){
   if (!node)
   return NULL;
   Node* temp = node->next;
   node->next = node->prev;
   node->prev = temp;
   if (!node->prev)
   return node;
   return Reverse(node->prev);
}
void printList(Node* head){
   while (head != NULL) {
      cout << head->data << " ";
      head = head->next;
   }
}
int main(){
   Node* head = NULL;
   push(&head, getNode(16));
   push(&head, getNode(1997));
   push(&head, getNode(10));
   push(&head, getNode(07));
   push(&head, getNode(2001));
   cout << "The Original List Is Here: ";
   printList(head);
   head = Reverse(head);
   cout << "\nThe Reversed Linked List Is Here: ";
   printList(head);
   return 0;
}

输出

The Original List Is Here: 2001 7 10 1997 16
The Reversed Linked List Is Here: 16 1997 10 7 2001

结论

在今天的文章中,我们学习了如何在 C++ 环境中实现构建和应用各种方法来从主数据集打印反转字符串的过程。通过上述逻辑、语法和算法;我们尝试构建一些 C++ 代码来有效地解决问题陈述。

更新于:2023年12月27日

253 次查看

开启你的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.