Description: The C language is not defined for string types, but there are many library functions about strings (such as: strstr (matching substrings in the original string), strcpy (copying the contents of the original string to the target string), strcmp (comparing the contents of two strings), strcat (connecting two strings, Also is the append of the string), strlen (the length of the string to be obtained) .... , we can write our own program to implement the functions of the function, and do not call library functions, and then against the library function to learn, is conducive to improve their own programming ability.
//1.strcat function (concatenate two strings, also append to string) #include <stdio.h> #include <stdlib.h > #include <assert.h>char *my_strcat (char *arr1,const char *arr2) {assert (ARR1); ASSERT (ARR2);char *ret = arr1;while (*ret) {ret++; The //ret pointer moves continuously until it is located at ' arr1 ' of the array}while (*ret++ = *arr2++) //continuously appends the contents of the ARR2 array to the ARR1, note: (The arr1 "\" will be overwritten by the first character in the ARR2, the last arr2 in the ' \ 0 ' is appended to the end of the arr1, otherwise there is no end flag in the array} {;} RETURN&NBSP;ARR1;} Int main () {char arr1[30] = "Wlcome"; // Be careful to leave enough space in the target string char arr2[] = " to china!"; My_strcat (ARR1,ARR2);p rintf ("%s\n", arr1); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/76/34/wKiom1ZMhtWBdK9_AAAbolAJ62Y992.png "title=" Run Result " alt= "Wkiom1zmhtwbdk9_aaabolaj62y992.png"/>
2.strcpy function (Copy the contents of the original string to the target string) #include <stdio.h> #include <stdlib.h> #include <assert.h>char *my_ strcpy (char *dest,const char *src) {assert (dest), assert (SRC), char *ret = dest;while (*ret++ = *src++)//constantly copy the contents of the original string to the target In the string {;} return dest;} int main () {char arr1[10]= "hello"; The original target string is "Hello", the copy is completed to "Welcome", the original string is overwritten with char arr2[10] = "Welcome"; my_strcpy (ARR1,ARR2);p rintf ("%s\n", arr1); System (" Pause "); return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/76/34/wKiom1ZMiE6jlMJuAAAWL7ndsA0557.png "title=" Run Result " alt= "Wkiom1zmie6jlmjuaaawl7ndsa0557.png"/>
//3.strstr function (matches substring in original string) #include <stdio.h> #include <stdlib.h># Include<assert.h>char *my_strstr (CHAR&NBSP;*SRC,&NBSP;CHAR&NBSP;*DST) {assert (DST); assert (SRC); char *p = src;char *q = dst;while ((*DST) && (*SRC)) {if (*SRC&NBSP;==&NBSP;*DST) //two pointer content is equal, the pointer moves backwards to match {src++;d st++;} else{src = ++p; //pointer content is unequal, the substring pointer jumps to the first address, and the original string returns to the next position where the match begins dst = q;p = &NBSP;SRC;}} if (*dst == ') ') //substring is ' + ' when the description match is complete, returning the original string with the same first address as the substring {return p;} else{return null; //no match, return empty}}int main () {char arr1[20] = " Wwwworld ";char arr2[20] = " Wworld "Char *ret=my_strstr (ARR1,ARR2);p rintf ("%s\n ", ret); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/76/33/wKioL1ZMilbzpJYkAAAXiE65AFw689.png "title=" Run Result " alt= "Wkiol1zmilbzpjykaaaxie65afw689.png"/>
4.strlen function (to find the length of a string, there are three ways) #include <stdio.h> #include <stdlib.h> #include <assert.h>// ①. Counter count string length int my_strlen (const char *str) {assert (str); int count = 0;while (*str++) {count++;} return count;} ②. Recursively calls the 1+ function itself (pointer is constantly offset) int My_strlen (const char *str) {assert (str); int num = 0;while (*str) {return num= (1+my_strlen (str + 1));} return num;} ③. The end of the pointer minus the initial position of the pointer and then minus one (the last position is ' + ') int my_strlen (const char *str) {assert (str); char *pre = Str;while (*pre++) {;} return (PRE-STR-1);} int main () {char arr[] = "hello,world!"; int Ret=my_strlen (arr);p rintf ("length=%d\n", ret); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/76/33/wKioL1ZMi7SQ17J3AAAaaFHCIvY413.png "title=" Run Result " alt= "Wkiol1zmi7sq17j3aaaaafhcivy413.png"/>
5.strcmp function (Compare the contents of two strings, here are two approaches, broadly similar, the biggest difference is that one method returns 0,-1,1 three values, the other method returns 0, or the difference between different characters in two strings) #include <stdio.h> #include <stdlib.h> #include <assert.h>//①. A method implementing INT&NBSP;MY_STRCMP with a return value of 0,-1,1 (const char * STR1,&NBSP;CONST&NBSP;CHAR&NBSP;*STR2) {assert (STR1); assert (STR2);while (*STR1&NBSP;==&NBSP;*STR2) The //Loop executes a condition of two pointers pointing to the same {if (*str1 == ') //If a pointer is pointing to ' The other is also ' 0{return ', stating that the two strings are the same, returning to the same (0);} str1++;str2++;} if ((*STR1&NBSP;-&NBSP;*STR2) > 0) //the condition for jumping out of a loop is that there are different characters, the return value is processed separately (1);elsereturn (-1);} ②. The return value is 0, or the difference between the different characters in two strings int my_strcmp (CONST&NBSP;CHAR&NBSP;*STR1,CONST&NBSP;CHAR&NBSP;*STR2) {assert (str1 ) && (STR2);while ((*STR1) && (*STR2)) // The conditions for loop execution are str1 and str2 are not '% ' End flag {if (*STR1&NBSP;==&NBSP;*STR2) //if the two pointers point to the same content, theThe pointer moves {str1++;str2++;} elsereturn * (unsigned char *) (str1) - * (unsigned char *) (STR2); //if the two pointers point to something different, the difference between the two characters is returned, because Char is consistent with the signed char in vs2013, so it can represent a maximum positive value of 127 and a minimum negative value of-128, but the ASCII code table shows that there are 256 characters. So the resulting difference will overflow and not be correctly represented, so convert it to Unsigned char form}if (*STR1&NBSP;==&NBSP;*STR2) && (*str1 == ' + ')) //the condition of jumping out of a loop to move at least one pointer in two strings to ' + ', if two pointers point to ' \ ' returns 0, otherwise returns the difference of two characters {return 0;} else{return * (unsigned char *) (str1) - * (unsigned char *) (STR2);} The return -1; //function finally has a return value, but no other exception, return a-1 can be}int main () {char arr1[] = "hello,world!"; char arr2[] = "hello,china!"; INT&NBSP;RET=MY_STRCMP (ARR1,ARR2);p rintf ("%d\n", ret); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/76/34/wKioL1ZMkjrTBE4NAAATVWbXMG8995.png "title=" Run Result " alt= "Wkiol1zmkjrtbe4naaatvwbxmg8995.png"/>650) this.width=650; src= http://s1.51cto.com/wyfs02/M00/76/34/ Wkiol1zmkkmbn3ldaaax_wpxxlc577.png "title=" Run Result "alt=" Wkiol1zmkkmbn3ldaaax_wpxxlc577.png "/>
This article is from the "Warm Smile" blog, please be sure to keep this source http://10738469.blog.51cto.com/10728469/1714503
Common string-handling function implementations (do not call library functions)