字串反轉程式(迭代和遞迴)


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 次檢視

開啟你的職業生涯

完成課程獲得認證

開始
廣告