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

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

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

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

Development

Wechat

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

12
Report