Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C language to program C++ rotation character operation string

Shulou Source: shulou.com Published: 2022-06-03 09:28:37 09月22日 Update

This article mainly introduces "how to use C language to program C++ rotation character operation string". In daily operation, I believe many people have doubts about how to use C language to program C++ rotation character operation string. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use C language to program C++ rotation character operation string". Next, please follow the editor to study!

Catalogue

Rotate string

String left-handed

Pre-topic cognition:

Violent displacement:

Three-step flip:

Judge string rotation

Pre-topic cognition

String append judgment

Rotate string string left

Implement a function that can rotate k characters in a left string.

For example:

ABCD left-hand spin one character to get BCDA

ABCD left-handed two characters to get CDAB

Pre-topic cognition:

A string is dead if it is. Eg:char arr [] = "dfdf" is so boring that there is no feeling of human-computer interaction at all. (although human-computer interaction is suitable for individuals and not suitable for clusters, it is also more comfortable than rigid fixed strings.)

Therefore, the string can be played only if it can be typed by us, and the incompleteness of the play will be cycled.

Violent displacement: # include#includechar* turn_left (char* arr, int num) {int len = strlen (arr); int I = 0; / / perform several shifts for (I = 0; I) as much as the left-handed number

< num; i++) { //每次移位就是单次左旋 int j = 0; char tmp = arr[0];//每次都把首元素存起来 for (j = 0; j < len-1; j++) { arr[j] = arr[j + 1]; } arr[len-1] = tmp;//把存起来的首元素放到最后一位 } return arr;}int main(){ char arr[100] = {0}; int num = 0; while (1) { printf("请输入想要旋转的字符串:>

"); gets (arr); printf (" Please enter the number of left turns you want: > "); scanf ("% d ", & num); getchar () / / the function of this is to clear the buffer, because every time scanf will have one, if it is not empty / / the next time it comes to gets, he will think that\ nWe typed in, so the scanf must empty the buffer printf ("left string is:% s\ n", turn_left (arr, num));} return 0;}

Output result

Three-step flip: # include#include#includevoid reverse (char* head, char* tail) / / string reverse {assert (head & & tail); / / assertion blocks null pointer char tmp = 0; char* ptmp = & tmp; while (head

Tags: Characters strings left-handed input languages language programming clocking programming functions buffers learning buffering cognition two man-machine functions mathematics methods violence more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Redmi MariaDB MySQL