About STR Family

Source: Internet
Author: User

Unrestricted string functions, such as strcpy, strcmp, and so on, our most commonly used string functions are unrestricted, just by looking at the null byte at the end of the string argument to determine its length. So what is a restricted string function? Let's take a look at the following example to learn more about

strcpy

Prototype:char *strcpy ( char *strdestination, const Char *strsource );

This function copies the parameter src string into the dest parameter, which needs to be noted when using this function, first of all, it is necessary to ensure that the target character array space is sufficient to accommodate the string to be copied, why? If the string is more than the number of leaders, the extra characters will still be copied, which will cause them to overwrite the value of the memory space that was previously stored behind the array. Second, the target parameter should be modifiable, so it must be a character array or a pointer to an array of dynamically allocated memory.

1.STRCPY Simulation Implementation:

Char *my_strcpy (char *dest, const char *src) {char *ret = Dest;assert (src! = null); assert (dest! = null); while (*dest++ = *s rc++) {;} return ret;}

strncpy: Copies the first n bytes of the string pointed to by the SRC address to the array referred to by dest, and returns DEST.  

The above is an unrestricted string function, so why is there a class of restricted string functions such as strncpy?

just take these two functions, strcpy just copies the string, but does not limit the number of copies, which can easily cause a buffer overflow. strncpy be a little safer. strncpy can select a character output, strcpy is not.

Here is the strncpy simulation implementation:

Char *my_strncpy (char *dest, const char *SRC, int n) {char *ret = Dest;assert (dest); assert (SRC); while (n--) {*dest++ = *src+ +;} *dest = ' + '; return ret;}


2. Connect two strings, we can use the STRCAT function

Function Prototypes:

Char *strcat ( char *strdestination, const char * strsource );

Function:

Add the string src refers to at the end of the Dest ("Dest" at the end of the cover).

Description

The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string.

Returns a pointer to the dest.

Char *my_strcat (char *dest, const char *src) {char *ret = Dest;assert (src! = null); assert (dest! = null); while (*dest) {dest+ +;} while (*dest++ = *src++) {;} return ret;}


Strncat:

Function

The string is concatenated by adding the first n characters of the string src refers to the end of the string referred to by dest and overwriting the ' dest ' at the end of the string.

Description

The memory areas referred to by SRC and dest cannot overlap, and dest must have enough space to accommodate the SRC string.

return value

Returns a pointer to the dest.

Char *my_strncat (char *dest, const char *SRC, int len) {char *ret = Dest;assert (dest); assert (SRC); while (*dest) {dest++;} while (len--) {*dest = *src;dest++;src++;} *dest = ' + '; return ret;}


3.strcmp function: Compares two strings.

One of the areas to be aware of IS

When S1<S2, returns a negative number;

When S1=s2, the return value = 0;

Returns a positive number when s1>s2.

That is: two strings are compared from left to right by character (by the ASCII value size) until different characters are present or "s" is encountered.

Some people think the return value is 1 and-1, respectively, is greater than and less than, if you think so, it is wrong. The ANSI standard specifies that the return value is positive, negative, 0. The exact value is dependent on the different C implementations.

int my_strcmp (const char *STR1, const char *str2) {assert (STR1), assert (STR2), while (*str1 = *str2) {if (*str1 = = ') ' Retur n 0;str1++;str2++;} if (*STR1-*str2 > 0) Return-1;elsereturn 1;}


strncmp function Prototypes:

int strncmp (const char * str1, const char * str2, size_t N);

Parameter str1, STR2 is the two string to compare, and N is the number of characters to compare.

The comparison of the string size is determined in the order of the ASCII code table, which is also the value of the character. STRNCMP () First S1 the first character value minus S2 First character value, if the difference is 0 then continue to compare the next character until the end of the character flag ' \ ', if the difference is not 0, the difference is returned. For example, the string "Ac" and "ba" comparisons return the difference between the characters "A" (65) and ' B ' (98) (-33). Note: The characters to be compared include the string end flag ' \ s ', and once you encounter '% ' the end comparison, no matter how much n is, no longer continues to compare the characters behind.


strncmp function Simulation Implementation:

int my_strncmp (const char *dest, const CHAR*SRC, int count) {assert (dest); assert (SRC); while (Count>0 && (*dest = = *SRC) {if (*dest = = ') ' Return 0;dest++;src++;count--;} return *dest-*src;}


4. In order to find a substring in a string, we can use the STRSTR function

Function Prototypes:

Char *STRSTR ( const char *string, const char *strcharset );

This function finds the starting position of the first occurrence of the entire S2 in S1 and returns a pointer to that location. If S2 does not appear completely anywhere in S1, the function returns a null pointer. If the second argument is an empty string, the function returns S1.


Char *my_strstr (const char *STR, const char *substr) {assert (str! = null); assert (substr! = null); char *s1 = (char *) Str;cha R *S2 = (char *) Substr;char *start = (char *) str;while (*start) {S1 = Start;while ((*s1! = ') && (*s2! = ') ' & amp;& (*s1 = = *s2)) {s1++;s2++;} if (*s2 = = ') return START;S2 = (char *) substr;start++;} return NULL;}


STRRSTR:

There is no such function in the standard library, and of course it can be easily implemented if needed

The STRRSTR function finds the last occurrence of the S2 in the string s1

char* my_strrstr (char const *S1, char const *S2) {char *last = NULL; Initialize the pointer to the previous matching location that was found char *current = Null;assert (S1), assert (S2), if (*S2)//Find only if the second string is not empty {current = (char *) strstr   (S1, S2);//s2 the first occurrence in S1 while (current! = NULL) {//each time the string is found, let the pointer point to its starting position, and then look for the string next match position last = current; Current = (char*) strstr (last + 1, s2);}} return last;}


5. The easiest way to find a specific character in a string is to use the STRCHR function

This function is relatively simple, the following is the simulation implementation:

#include <stdio.h> #include <stdlib.h>char *my_strchr (const CHAR*STR, int ch) {while (*STR) {if (*str = ch) Return (char*) str;str++;} return NULL;} int main () {char *str = "Happy"; char *ret = MY_STRCHR (str, ' a '); if (ret! = NULL) {printf ("%c\n", *ret);} System ("pause"); return 0;}


STRRCHR:

The strrchr () function looks for the first occurrence of a character in the specified string, and if successful, returns all characters from that position to the end of the string, or False if it fails. Corresponding to this is the strstr() function, which finds the position of the first occurrence of the specified character in the string.

Function Prototypes:

Char *STRRCHR ( const char *string, int c );

Char *my_strrchr (char *str, char ch) {Char *ptr = Null;assert (str); while (*STR) {if (*str = ch) ptr = str;str++;} if (ptr! = 0) return Ptr;return 0;} int main () {char ch = 0;char *ret = Null;char *arr = "FEABADC";p rintf ("Please enter a character you want to find:"); scanf_s ("%c", &ch); ret = MY_STRR Chr (arr, ch);p rintf ("%s\n", *ret); System ("pause"); return 0;}


Of course, there are a lot of functions about STR, and here the simulation implements some functions that are often used, hoping to learn from each other's help.

This article is from the "Years Quiet good" blog, please make sure to keep this source http://01160529.blog.51cto.com/11479142/1835029

About STR Family

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.