Implementation of the function of the non-restricted string function

Source: Internet
Author: User

1. String length function prototype:

size_t strlen (char const *string);

The length of the strlen () calculation does not include '/'.

The value returned by strlen is an unsigned integer, so the result like if (strlen (x)-strlen (y) >=0) is absolutely true and will not reach the result you want.

To calculate the implementation of a string length:

#include <stdio.h> #include <stdlib.h>int my_strlen (char *p) {                  int count = 0;                  while (*p  !=  ' + ')                   {                          count++;                          p++;                  }                  return count;} Int main () {                  char arr [100];                  scanf (  "%s",  arr);                  int  ret = my_strlen (arr);                  printf (  "%d\n",  ret);                  system (  "Pause" );                  return  0;}

2. String copy function prototype:

Char *strcpy (char *dst,char const *SRC);

The strcpy () function copies the characters in the parameter src string into the parameter DST, and when the parameter DST and parameter src memory overlap, the result is undefined.

Attention:

1, strcpy () met ' \ ' to stop, so when the parameter src string is longer than the parameter DST string, extra characters will overwrite the memory space behind the parameter DST string, which is very dangerous.

2. When the string in SRC is shorter than DST, the old string after the new string will be deleted because strcpy () will copy the "\" in Src.

Char arr[]= "Hello World";

strcpy (Arr,welcome);

W E L C O M E 0 R L
D 0


As a table, the new string meets '% ' to stop, and the following character is the equivalent of being deleted.

#include <stdio.h> #include <stdlib.h>void my_strcpy (CHAR&NBSP;*P1,&NBSP;CHAR&NBSP;*P2) {                  while  (* p2 !=  ' + ')                  {                                 * p1  = *p2 ;                                   p1++;                                  p2++;                 }                 * p1 =  '  ;} Int main () {                  char arr1[100];                  char arr2[50];            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "%s", &NBSP;ARR1);        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "%s", &NBSP;ARR2);                  my_strcpy (ARR1,&NBSP;ARR2) ;                  printf (   "%s\n", &NBSP;ARR1);                 system (  "pause");                  return 0;}

3. String Connection function prototype:

Char *strcat (char *dst,char *src);

The strcat () function is the string in src that is appended to the string in DST, and the result is undefined if the src and DST locations overlap.

Note: When using the strcat () function, it is important to consider whether the remaining space after the original string in the DST array can hold SRC.

The implementation of the string connection "function":

#include <stdio.h> #include <stdlib.h>void my_strcat (CHAR&NBSP;*P1,&NBSP;CHAR&NBSP;*P2) {                  while  (* p1 )                 {                                   p1++;                 }                  while  (*p2 )                  {                                  * p1 = *p2 ;                                   p1++;                                   p2++;                 }                 * p1 =  '  ;} Int main () {                  char arr1[100];                  char arr2[50];      &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "%s%s", &NBSP;ARR1,&NBSP;ARR2);                 my_strcat (ARR1,&NBSP;ARR2) ;                 printf (  "%s\n", &NBSP;ARR1);                 system (  "pause");                  return 0;}

4. String comparison function prototype:


int strcmp (char const *s1,char const *S2);


The string compares the ASCII value, compares the characters in the two strings, finds the first unmatched character, and returns a number greater than 0 if the ASCII in S1 is greater than S2, and returns a number less than 0 if it is less than. If the string in S1 is shorter than the S2, a number less than 0 is returned.

If the match continues until ' \ ', two strings are equal, then 0 is returned.

The implementation of the string comparison "feature":

#include <stdio.h> #include <stdlib.h>int my_strcmp (CHAR&NBSP;*P1,&NBSP;CHAR&NBSP;*P2) {        if  (*P1&NBSP;==&NBSP;*P2)        {             while  (*P1&NBSP;==&NBSP;*P2)             {                 if  (*p2 ==  ')                     return 0;                  p1++;                  p2++;              }             if  (*P1&NBSP;&GT;&NBSP;*P2)                  return 1;             else                 return -1;      }}int main () {     char &NBSP;ARR1[100];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR2[100];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "% s%s ", &NBSP;ARR1,&NBSP;ARR2);      int ret = my_strcmp (ARR1,ARR2);      printf (  "%d\n",  ret)      system (  "pause");      return 0;}


5. Find a character function prototype:

Char *strchr (char const *str,int CH);

Char *strrchr (char const *str,int CH);

STRCHR () is the location where the first occurrence of the lookup occurs, and STRRCHR () is the last occurrence of the lookup. Returns a pointer to this location if found, and returns null if it is not found.

Find a character the first time the implementation of a "feature" appears:

#include <stdio.h> #include <stdlib.h>char *my_strchr (CHAR&NBSP;*P,&NBSP;CHAR&NBSP;CHR) {        while  (*p )       {             if  (*P&NBSP;==&NBSP;CHR)                 return p ;              p++;      }       return null ;} Int main () {     char arr[100];     char chr;      printf (  "input is found to make my character \ n"  ); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "%c" , &NBSP;&AMP;CHR);      printf (  "Enter the string to find \ n"  );      scanf (  "%s", arr); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;*RET=MY_STRCHR (ARR,&NBSP;CHR)      printf (  "%s\n",  ret);      system (  "Pause");      return 0;}

"find the implementation of the last occurrence of a character" feature:

#include <stdio.h> #include <stdlib.h>char *my_strrchr (CHAR&NBSP;*P,&NBSP;CHAR&NBSP;CHR) {    char *p2 = NULL ;   while  (*p )       {          if  (*P&NBSP;==&NBSP;CHR)             p2 = p;      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P++;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;RETURN&NBSP;P2;} Int main () {    char arr[100];    char chr;     printf (  "Enter the characters found \ n"  ); &NBSP;&NBSP;&NBSP;&NBSP;SCANF (  "%c", &NBSP;&AMP;CHR);     printf (  "input found string \ n"  ),     scanf (  "%s",  arr);  &NBSP;&NBSP;&NBSP;CHAR&NBSP;*RET&NBSP;=&NBSP;MY_STRRCHR (ARR,&NBSP;CHR);     printf (  "% S\n ",  ret);    &Nbsp;system (  "pause");     return 0;} 

6. Find whether a string is a substring of another string prototype:

Char *strstr (char const *s1,char const *S2);

This function finds the first occurrence of the entire S2 in S1, returns the address if found, returns null if it is not found, and returns S1 if S2 is an empty string.

Find the implementation of a string "feature":

#include <stdio.h> #include <stdlib.h>char *my_strstr (CHAR&NBSP;*P1,&NBSP;CHAR&NBSP;*P2) {      if  (*p2 == null)           return p1 ;     while  (*p1 )      {         if  (*P1&NBSP;==&NBSP;*P2)          {             char *p3  = p1 ;             char *p4  = p2 ;             while   (*P3&NBSP;==&NBSP;*P4)              {                  if  (*p4 ==  ' &NBSP;)                     return p1 ;                   p3++;                   p4++;               }             if   (*p4 ==  '  )                  return p1 ;        }         p1++;     }    return  null ;} Int main () {    char arr1[100];    char arr2[100];  &NBSP;&NBSP;&NBSP;SCANF (&NBSP; " %s%s ", &NBSP;ARR1,&NBSP;ARR2);     int  ret = my_strstr (ARR1,&NBSP;ARR2) ;     printf (  "%d\n",  ret)     system (  "pause");     return 0;}


This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1755434

Implementation of the function of the non-restricted string function

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.