[C++] string反转的5种方法 |
您所在的位置:网站首页 › 字符串反转代码 › [C++] string反转的5种方法 |
Different methods to reverse a string in C/C++ Given a string, write a C/C++ program to reverse it. 1. Write own reverse function by swapping characters: One simple solution is to write our own reverse function to reverse a string in C++ // A Simple C++ program to reverse a string #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 string str = "geeksforgeeks"; // Reverse str[begin..end] reverse(str.begin(), str.end()); cout string s = "GeeksforGeeks"; reverse(s); return (0); }4. Getting reverse of a const string: // 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 string str = "GeeksforGeeks"; //Use of reverse iterators string rev = string(str.rbegin(),str.rend()); cout |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |