Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize string inversion in C language

Shulou Source: shulou.com Published: 2022-06-02 16:13:36 10月04日 Update

This article mainly introduces how to achieve string inversion in C language, which is very detailed and has a certain reference value. Friends who are interested must read it!

Code implementation # include # include # define MAX_LENGTH 50 / / maximum length char * String_Invertion (char * str); int main () {char string [Max _ LENGTH]; printf ("Please enter a string\ n"); scanf ("% s", string); String_Invertion (string); printf ("inverted string is% s\ n", string); return 0 } / * @ brief string inversion function * @ param str target string * @ return inverted string (str) * / char * String_Invertion (char * str) {if (str = = NULL) return NULL; int len = strlen (str); int I = 0; for (I = 0; I < len / 2) Replace len/2 times {/ * (bitwise) XOR exchange, other exchange methods refer to day27 * / str [I] = str [I] ^ str [len-1-I]; str [len-1-I] = str [I] ^ str [len-1-I]; str [I] = str [I] ^ str [len-1-I];} return str;}

Running result

Online reference / / Created by www.runoob.com on 15 Created by www.runoob.com on 11 Universe 9.Compact / Copyright ©2015 rookie tutorial. All rights reserved.//#include void reverse (char* s) {/ / get string length int len = 0; char* p = s; while (* p! = 0) {len++; paired strings;} / / exchange. Int I = 0; char c; while (I

Tags: Characters strings references language content articles length maximum code value interests functions guys buddies tutorials more goals knowledge results rookie tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Microsoft Apple macOS