How to rotate the string left

Source: Internet
Author: User
Tags include

Topic Description:

In assembly language There is a shift instruction called cyclic left-shift (ROL), now has a simple task, is to use strings to simulate the operation of this instruction results. For a given sequence of characters s, ask you to shift its loop left K-bit after the sequence output. For example, the character sequence s= "Abcxyzdef", requiring the output loop to move left 3 digits after the result, namely "XYZDEFABC". Is it simple? OK, take care of it!

Input:

Multiple sets of test data, each containing a character sequence s and a nonnegative integer k. The length of which s is not more than 1000.

Output:

corresponding to each test case, output a new sequence.

Sample input:

Udboj 4

ABBA 1

Sample output:

Judbo

Bbaa

This problem was previously done on the cracking the coding interview similar, the same three times inversion, you can get, time complexity of O (n), space complexity of O (1). This problem in nine degrees OJ test, mainly to consider moving the number of digits greater than the length of the string, the rest can be.

The AC code is as follows:

 #include <stdio.h> #include <string.h> void Swap (char *a,char *b) {int temp = *a;  
    *a = *b;  
*b = temp;  
    } * * This part of the flip string from start to end/void ReverseString (char *str,int start,int end) {while (Start < end)  
        {Swap (&str[start],&str[end]);  
        start++;  
    end--; }/* Find the characters after the left K bit more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/*/void rolstring  
(Char *str,int k)  
          
    {if (str = NULL | | k<=0) return;  
    int len = strlen (str);  
    To consider K greater than Len's case k = K%len;  
      
    if (k = = 0) return;  
    ReverseString (str,0,k-1);  
    ReverseString (str,k,len-1);  
ReverseString (str,0,len-1);  
    int main () {char str[1010];  
    int k;  
        while (scanf ("%s%d", str,&k)!= EOF) {rolstring (str,k);  
    Puts (str);  
return 0; }

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.