"C language" left-handed string

Source: Internet
Author: User

Implement a function that can be left-handed in the string of K-character method one: Open another array, first write the character after k+1 to the array, and then write the K-character to the left to achieve the following: #include <stdio.h> #include < assert.h> #include <string.h>void left_relvove (char *str,char *arr,int k) {Char  *pcur = str+k;     //let the pointer begin with the K+1 character and save it in the ARR array      assert  (Str && arr);         //assert while (*pcur)             //starts with the k+1 character and encounters a ' \ ' jump out {*arr++ =  *pcur++;   }pcur = str;  //after writing the character after k+1 to arr, move the pointer to the first element of STR, Start writing the k character to rotate while (k) {*arr++ = *pcur++;k--;} *arr= ' + ';} Int main () {    int k;    char str[10] = {0}; &NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR[10]&NBSP;=&NBSP;{0};&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &k); scanf ("%s", str);     left_relvove (str,arr,k); &NBSP;&NBSP;&NBSP;&NBSP;PRintf ("%s\n", arr);     return 0;} Method Two: Define a string rollover function, the left-handed K-word Mr. Foo reverse reversal, and then the k+1 after the reverse reversal of the character, and finally the entire string reversed reverse implementation as follows: #include <stdio.h> #include  < String.h>void reserve (char *left,char*right)  {    while  (left  < right)     {        char tmp =  *left;        *left = *right;         *right = tmp;        left++;         right--;    }}int main () {     char arr[10] =  "AABCD";      int len = strlen (arr) -1;    int k = 0;    char *pstart =  &arr[0];    char *pend = arr+len; &NBSP;&NBSP;&NBSP;SCANF ("%d",  &k);     reserve (pstart, pstart + k &NBSP;-1);  //to the left-handed K word Mr. Foo reverse reversal     reserve (pstart + k, pend);       //k+1 The characters after the reverse     reserve (pstart, pend);           //the entire string reversed reverse     printf ("%s\n",  arr);     system ("pause");     return 0;}


This article is from the "vs LV" blog, so be sure to keep this source http://survive.blog.51cto.com/10728490/1709777

"C language" left-handed 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.