[C Language] Left-hand string

Source: Internet
Author: User

[C Language] Left-hand string

To implement a function, you can use k characters in the left-hand string. Method 1: open another array and write the characters After k + 1 into this array first, write the k characters to be left-handed as follows: # include <stdio. h> # include <assert. h> # include <string. h> void left_Relvove (char * str, char * arr, int k) {char * pcur = str + k; // first let the pointer start from the k + 1 character, in the arr array, assert (str & arr); // assert while (* pcur) // starts with the k + 1 character, when '\ 0' jumps out of {* arr ++ = * pcur ++;} pcur = str; // writes the character after k + 1 to arr, adjust the pointer to the first element of str and start writing the k characters to be rotated while (k) {* arr ++ = * pcur ++; k --;} * arr = '\ 0';} int main () {int k; char str [10] = {0}; char arr [10] = {0 }; scanf ("% d", & k); scanf ("% s", str); left_Relvove (str, arr, k); printf ("% s \ n ", arr); return 0;} Method 2: Define a string flip function, first Reverse flip the k characters to be left, and then reverse flip the characters After k + 1, finally, flip the entire string in reverse order 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; scanf ("% d", & k); reserve (pstart, pstart + k-1 ); // first reverse the k characters to be left-handed to reserve (pstart + k, pend); // reverse the character after k + 1 to reserve (pstart, pend ); // Reverse flip of the entire string printf ("% s \ n", arr); system ("pause"); return 0 ;}

 

Related Article

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.