logo

C++ で文字列を反転するさまざまな方法

文字列の反転とは、単に文字列の最後の要素を文字列の最初の位置に置き換えることに他なりません。

文字列の反転



C++ で文字列を反転するさまざまな方法は次のとおりです。

  • 独自のリバース関数を作成する
  • 「内蔵」リバース機能の使用
  • コンストラクターの使用
  • を使って 温度 ファイル

1. 文字を入れ替えるためのカスタム逆関数の作成

  • 最初から最後までのアプローチを使用する ' のために' ループ

CPP








// C++ program to reverse a string> // using first to last approach> // 'for' loop> #include> 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 swap(str[i], str[n - i - 1]); } // Driver program int main() { string str = 'geeksforgeeks'; reverseStr(str); cout << str; return 0; }>

>

>

出力

skeegrofskeeg>

複雑さの分析:

時間計算量: の上)
補助スペース: ○(1)

  • while ループを使用した first to last アプローチの使用

C++




// C++ program to reverse a string> // using while loop> #include> 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;> }>

>

>

出力

skeegrofskeeg>

複雑さの分析:

時間計算量: の上)
補助スペース: ○(1)

  • 最後から最初へのアプローチを使用する ' のために ループ

C++




浮動小数点から文字列へ
// C++ program to demonstrate reverse> // of a string using Last to First> // Approach 'for' Loop> #include> using> namespace> std;> // Function to reverse a string> void> reverse(string str)> {> >for> (>int> i = str.length() - 1; i>= 0;私--)>> >cout << str[i];> }> // Driver code> int> main(>void>)> {> >string s =>'techcodeview.com'>;> >reverse(s);> >return> (0);> }>

>

>

出力

skeeGrofskeeG>

複雑さの分析:

時間計算量: の上)
補助スペース: ○(1)

  • Last to First アプローチの「while」ループの使用

C++




// C++ program to demonstrate reverse> // of a string using Last to First> // Approach 'while' Loop> #include> 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 =>'techcodeview.com'>;> >reverse(s);> >return> (0);> }>

>

>

出力

skeeGrofskeeG>

複雑さの分析:

時間計算量: の上)
補助スペース: ○(1)

メイクアップ製品名

1. 2 ポインターアプローチによる再帰関数の使用

再帰関数は、文字列のさまざまなインデックスを反復処理するために使用されます。

C++




// C++ program to reverse a string> // using recursion> #include> using> namespace> std;> // Function to reverse a string> void> reverseStr(string& str,>int> n,>int> i)> {> > >if>(n<=i){>return>;}> // Swapping 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;> }>

>

>

出力

skeegrofskeeg>

複雑さの分析:

時間計算量: の上)

補助スペース : の上)

2. 再帰での 1 ポインター アプローチの使用

コードの実装は次のとおりです。

C++




//C++ program to reverse a string using recursion> #include> 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>

>

>

出力

skeegrofskeeg>

複雑さの分析:

時間計算量: の上)

補助スペース: の上)

3. 内蔵リバース機能の使用

アルゴリズム ヘッダー ファイルには、プログラミングの時間を節約するリバースを実行するための直接関数があります。

// Reverses elements in [begin, end] void reverse (BidirectionalIterator begin,  BidirectionalIterator end);>

CPP




// C++ program to illustrate the> // reversing of a string using> // reverse() function> #include> using> namespace> std;> int> main()> {> >string str =>'geeksforgeeks'>;> >// Reverse str[begin..end]> >reverse(str.begin(), str.end());> >cout << str;> >return> 0;> }>

スープ
>

>

出力

skeegrofskeeg>

複雑さの分析:

時間計算量: の上)

補助スペース: ○(1)

4. コンストラクターを使用して文字列を反転する

逆反復子をコンストラクターに渡すと、反転された文字列が返されます。

CPP




// C++ program to reverse> // string using constructor> #include> using> namespace> std;> int> main()> {> >string str =>'techcodeview.com'>;> >// Use of reverse iterators> >string rev = string(str.rbegin(), str.rend());> >cout << rev << endl;> >return> 0;> }>

>

>

出力

skeeGrofskeeG>

複雑さの分析:

時間計算量: の上)

補助スペース: ○(1)

5. 一時文字列の使用

CPP




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

>

>

出力

skeeGrofskeeG>

複雑さの分析:

時間計算量 : の上)

補助スペース: ○(1)

const 文字列の逆を取得するにはどうすればよいでしょうか?

const 文字列の逆を取得するには、最初に ‘ を宣言する必要があります 定数文字列」 ユーザー定義関数で宣言した後、目的のオブジェクトを呼び出すために次のアルゴリズムを使用します。

const reverseConstString = function(string) { return string.split('').reverse().join('')>

例:

C++




// C++ program to get reverse of a const string> #include> 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 swap(rev[i], rev[j]); // return pointer of the reversed string return rev; } // Driver code int main(void) { const char* s = 'techcodeview.com'; printf('%s', reverseConstString(s)); return (0); }>

>

>

出力

skeeGrofskeeG>

時間計算量: の上)
補助スペース: の上)

スタックデータ構造の使用

C++




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

>

>

出力

Javaのstring.format
skeeGrofskeeG>

複雑さの分析:

時間計算量: の上)

補助スペース: の上)

ベクトルデータ構造の使用

C++




// C++ Program to reverse a string> #include> using> namespace> std;> int> main()> {> >string s =>'techcodeview.com'>;> >int> n=s.length();> >vector<>char>>もの;>>' for> (>int> i = n - 1; i>= 0;私--)>> >vec.push_back(s[i]);> > >for>(>auto> i:vec){> >cout< } return 0; }>

>

>

出力

skeeGrofskeeG>

複雑さの分析:

時間計算量: の上)

補助スペース: の上)

この記事は次の寄稿者です プリヤム・カカティ、ランジュ・クマリ、ソメーシュ・アワスティ。