Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of C++ string inversion

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what are the methods of C++ string inversion". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

/ * Analysis process: assume that the passed string is a constant const string, so that the operation is more flexible, and the literal value of the string can be passed directly for reversal. The common solution is to define a new character array equal to the length of the passed string, then copy the string and place the str characters into the character array from left to right. Then use a loop to reverse * / / * characters in the character array. First, using the above solution, pass to the const type C-style character pointer, return to the char* type * / / directly use the character array assignment char* strrev1 (const char* str) {const size_t length = strlen (str) / / find the character length char * temp = new char [length]; / / create a new character array of equal length strcpy (temp,str); / / copy for (size_t I = 0; I tmp) {char t = * tmp; * tmp++ = * p; * Plam-= t;} return ret } / / is basically similar to the previous function, except that this function uses shift operations to change the character pointer to char* strrev3 (const char* str) {char* tmp = new char [strlen (str) + 1]; strcpy (tmp,str); char* ret = tmp; char* p = tmp + strlen (str)-1; while (p > tmp) {* p ^ = * tmp * tmp++ ^ = * p; * pmurf-^ = * tmp;} return ret;} / / Save a few steps and directly assign an inverted value to the newly created character array. Hehe, it is simple and clear, but the loop executes char* strrev4 (const char* str) {char* temp = new Charlen (str) several times; for (int I = 0; I c_str (); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report