Shifts the left of a string Loop

Source: Internet
Author: User

Enter a string and a non-negative integer N, and the string must be shifted left n times.

Input Format:

The input row contains a non-empty string of no more than 1st characters ending with a carriage return. The input row contains a non-negative integer n.

Output Format:

Output the string after N cycles are shifted left in one row.

Input example:
Hello World!2
Output example:
llo World!He

# Include <stdio. h>
# Include <string. h>

Int main () {char s [101], S2 [101];
Int N;
Gets (s );
Char * P = s;
Scanf ("% d", & N );
N = n % strlen (s); // left shift length judgment
P = P + N;
Strcpy (S2, P); // copy the flag to S2 on the right
// S2 [N] = '\ 0 ';
* P = '\ 0'; // s to the string on the left of the flag
// Strcpy (S, P );
Strcat (S2, S); // The Connection operation is completed and moved left;
Puts (S2 );
Return 0;
}

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. int main(void){
  5.     const int length_of_array = 101;                // Define the array Length
  6.     char sentence[length_of_array];
  7.     char *temporary = (char*)malloc(sizeof(char)*length_of_array);   // Temporary memory space
  8.     int shift;                         // Offset
  9.     gets(sentence);
  10.     scanf("%d",&shift); 
  11.     int length_of_sentence = strlen(sentence);           // Obtain the length of the input data
  12.     if ( shift > length_of_sentence){                    // When the processing offset is greater than the Data Length
  13.         shift = shift % length_of_sentence;
  14.     } 
  15.    
  16.     char *located_address = sentence + shift;  // This address is the address after shift offset relative to the original address"
  17.       
  18.     strcpy(temporary,located_address);      // Copy all the contents of the Offset address to the temporary container.
  19.     *located_address = ‘\0‘;          // Change the data corresponding to the offset address to '\ 0' for use below
  20.     strcat(temporary,sentence);         // The data content of the original address is directly appended to the temporary space.
  21.         printf("%s",temporary); free(temporary);        
  22.     return 0;
  23. }

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.