The loop of a miscellaneous question moves a string

Source: Internet
Author: User

1. Problem: A M-character string, ' Loop ' Moves the N (n <= m) bit to the left or right, seeking the moved string. For example, the ABCDEFG left loop moves 3-bit, DEFGABC, and the loop moves three-bit-EFGABCD.

2. There are many implementations, the most straightforward but inefficient approach is to move the character elements one after the other, similar to moving an array. Here is a technique for implementing a loop left-shift n-bit, for example :

1). First turn over the first n, at this time you can extend your right hand, palms to your own, assuming that at this time from the nameless finger to thumb marking for 1,2,3,4,5, and then flip your right hand, let the palms outside, then this is 5,4,3,2,1. The process of flipping the top n-bit string is similar.

So how does this process in the program be implemented, very simple, is the first and finally think of the swap, the second and the second-to-last swap, and so on, and so on, the implementation code is as follows:

Static voidSwap (Char&a,Char&b) {a= a ^b; b= a ^b; A= a ^b;}Static voidMove (Char*STR,intHeadinttail) {    if(str = = NULL | | strlen (str) <2)    {        return; }     for(; head < tail; head++, tail--) {Swap (* (str + head), * (str +tail)); }}

2). Flip the n+1 to M-bit, similar to the top of the process. Of course, there are no sequential differences between the two flip execution sequences.

3). After the whole string is flipped, it gets the string after the left shift of the loop.

The process of looping right shifts is similar.

3. Below the code (MOVE.C) that you can choose to cycle left and right, the reader can refer to this understanding:

#include <stdio.h>#include<stdlib.h>#include<string.h>Static voidSwap (Char&a,Char&b) {a= a ^b; b= a ^b; A= a ^b;}Static voidMove (Char*STR,intHeadinttail) {    if(str = = NULL | | strlen (str) <2)    {        return; }     for(; head < tail; head++, tail--) {Swap (* (str + head), * (str +tail)); }}intMain (intargcChar*argv[]) {    Charstr[ -] = {0 }; intMovenum; CharDirec;  while(1) {printf ("Enter A string and move direction (L or R) and a move num\n"); if(SCANF ("%s%c%d", str, &AMP;DIREC, &movenum) = =3)        {            if(Movenum <0|| Movenum >strlen (str)) {printf ("error\n"); Continue; } printf ("before Move:%s \ n", str); if('R'==Direc) {                //The sequence of both move op can be changedMove (str, strlen (str)-Movenum, strlen (str)-1); Move (str,0, strlen (str)-Movenum-1); }            Else if('L'==Direc) {                //The sequence of both move op can be changedMove (STR,0, Movenum-1); Move (str, movenum, strlen (str)-1); }            Else            {                Continue; } move (str,0, strlen (str)-1); printf ("After Move:%s \ n", str); }        Else        {             Break; }    }    return 0;}

Compile command: g++-g-o move move.c;, run:./move

Partial Run Result:

  

When let's just a simple example program, if the code is wrong, please correct it, and if there is a better way, hope to enlighten.

  

The loop of a miscellaneous question moves a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.