C Language String length and string inversion

Source: Internet
Author: User

What I want to share today is a write about the string, so let's start with the simplest. The main function defines a string variable, and we want to know the length of the string. The length of a string usually refers to the number of characters contained in the string, which we can get by invoking the library function, or by writing a code ourselves.

First we define a string array in the main function, char *qwe= "abcdef", and then define two pointers P,q point to the first address of the string . p Pointer does not move always pointing to the first address when the contents of the Q pointer! = "\" when its address has been Gaga, when its content = "" ", with the address pointed to by the Q pointer minus the address pointed to by the P pointer, the length of the string is obtained.

#include <stdio.h> #include <string.h>int my_strlen (char *xsw) {char *q=xsw;char *p=xsw;while (*q!= ') {Q
}return (q-p);} int main () {char *xsw= "zxcvbnm"; int Ret=my_strlen (XSW);p rintf ("%d", ret); return 0;}

By sharing above presumably we already know how to write the length of a code output string ourselves. The next thing I want to share is how to invert the output of a string.

First we define a string array, char arr[]= "abcdef", then define two pointers char* left,char* right,left Pointer to the first address of the string, the right pointer to the address of the last character of the string, Assert that the left pointer and the right pointer exist, and if so, when the left pointer points to an address less than or equal to the address pointed to by the right pointer, we define a variable char tmp, using a similar interchange method, to first place the contents of the left pointer to TMP. Put the right pointer to the left, and finally put the contents of the left pointer in the TMP into right, the left pointer address Gaga, the address of the right pointer minus. This allows us to implement a string reversal.

#include <stdio.h> #include <string.h> #include <assert.h>void reserve (char *left,char *right) { ASSERT (left), assert (right), while (left<=right) {char tmp=*left;*left=*right;*right=tmp;left++;right--;}} void Left_move (char *arr, int n,int len) {reserve (arr,arr+n-1); reserve (arr+n,arr+len-1); reserve (arr,arr+len-1);} int main () {char arr[]= "abcdef"; int len = strlen (arr); Left_move (Arr,3,len);p UTS (arr); return 0;}


This article is from "Dream" blog, please be sure to keep this source http://12951882.blog.51cto.com/12941882/1976417

C Language String length and string inversion

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.