Left-hand string

Source: Internet
Author: User
Description:
In assembly language, there is a shift instruction called ROL. Now a simple task is to simulate the operation result of this instruction using strings. For a given Character Sequence S, please shift its loop left K-bit sequence output. For example, the Character Sequence S = "abcxyzdef" requires that the output result of the loop be shifted three places left, that is, "xyzdefabc ". Is it easy? OK, get it done!
Input:
Multiple groups of test data. Each test data contains a Character Sequence s and a non-negative integer k. The length of S cannot exceed 1000.
Output:
Output a new sequence for each test case.
Sample input:
UDBOJ 4abba 1
Sample output:
JUDBObbaa
Ideas:
Similar to the order of flipped words:
1. First flip the first n characters;
2. After turning it over, len-n characters are returned;

3. Rotate it as a whole.

/* Left-hand string by rowandjj2014/8/16 */# include <stdio. h> # include <string. h> # include <stdlib. h> void reverse (char * pbegin, char * pend) {If (pbegin = NULL | pend = NULL) {return;} Char temp; while (pbegin <pend) {temp = * pbegin; * pbegin = * pend; * pend = temp; pbegin ++; pend --;}} // The number of left-handed characters whose pdata is the string n void leftrotatestring (char * pdata, int N) {If (pdata = NULL | n <= 0) {return ;} int Len = strlen (pdata); If (LEN <= 0) {return;} n = N % Len; // rotate the first n characters of reverse (pdata, pdata + N-1); // rotate the following len-n characters of reverse (pdata + N, pdata + len-1); // The overall rotation reverse (pdata, pdata + len-1);} int main () {char STR [1000]; int N; while (scanf ("% S % d", STR, & N )! = EOF) {leftrotatestring (STR, n); printf ("% s \ n", 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.