Rôzne metódy na obrátenie reťazca v C++
Obrátenie struny nie je nič iné ako jednoduché nahradenie posledného prvku struny na 1. pozíciu struny.
Rôzne metódy na obrátenie reťazca v C++ sú:
- Vytvorenie vlastnej reverznej funkcie
- Použitie „vstavanej“ spätnej funkcie
- Pomocou konštruktora
- Pomocou a tepl súbor
1. Vytvorenie vlastnej reverznej funkcie na výmenu znakov
- Použitie prístupu prvý pred posledným“ pre slučka
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; }> |
Výkon
skeegrofskeeg
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
- Použitie prístupu od prvého k poslednému so slučkou while
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;> }> |
Výkon
skeegrofskeeg
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
- Použitie prístupu od posledného k prvému“ pre „Slučka
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; i--)> > cout < < str[i];> }> // Driver code> int> main(> void> )> {> > string s => 'techcodeview.com'> ;> > reverse(s);> > return> (0);> }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
- Použitie slučky medzi posledným a prvým prístupom „počas“.
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);> }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
1. Použitie rekurznej funkcie s prístupom dvoch ukazovateľov
Rekurzné funkcie sa používajú na iteráciu do rôznych indexov reťazca.
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;> }> |
Výkon
skeegrofskeeg
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor : O(N)
2. Použitie prístupu jedného ukazovateľa v rekurzii
Nižšie je uvedená implementácia kódu:
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> |
Výkon
skeegrofskeeg
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(N)
3. Použitie vstavanej spätnej funkcie
V hlavičkovom súbore algoritmu je priama funkcia na vykonanie spätného chodu, ktorá nám šetrí čas pri programovaní.
// 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;> }> |
Výkon
skeegrofskeeg
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
4. Obráťte reťazec pomocou konštruktora
Odovzdanie reverzných iterátorov do konštruktora nám vráti obrátený reťazec.
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;> }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(1)
5. Použitie dočasného reťazca
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; i--)> > rev.push_back(str[i]);> > cout < < rev < < endl;> > return> 0;> }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť : O(N)
Pomocný priestor: O(1)
Ako by sme mohli získať opak const reťazca?
Aby sme získali opak const reťazca, musíme najprv deklarovať „ const string' v užívateľom definovanej funkcii, ktorú sme deklarovali, potom použite nasledujúci algoritmus na volanie požadovaných objektov.
const reverseConstString = function(string) { return string.split('').reverse().join('') Príklad:
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); }> |
Výkon
skeeGrofskeeG
Časová zložitosť: O(N)
Pomocný priestor: O(N)
Používanie štruktúry údajov zásobníka
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;> }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(N)
Použitie vektorovej dátovej štruktúry
C++
// C++ Program to reverse a string> #include> using> namespace> std;> int> main()> {> > string s => 'techcodeview.com'> ;> > int> n=s.length();> > vector <> char> >vec;> > for> (> int> i = n - 1; i>= 0; i--)> > vec.push_back(s[i]);> > > for> (> auto> i:vec){> > cout < } return 0; }> |
Výkon
skeeGrofskeeG
Analýza zložitosti:
Časová zložitosť: O(N)
Pomocný priestor: O(N)
Do tohto článku prispeli Priyam Kakati, Ranju Kumari, Somesh Awasthi.