Day 16: string function

The content of these two days is becoming more and more difficult, and it takes more time to understand the implementation of functions. Although half of today's knowledge has been briefly mentioned before, it still takes some time to really master it.

Find string length

strlen

String function with unlimited length

strcpy

strcat

strcmp

Introduction to string functions with limited length

strncpy

strncat

strncmp

String lookup

strstr

strtok

Error message report

strerror

Function introduction

strlen

size_t strlen ( const char * str );

The string has' \ 0 'as the end flag. The strlen function returns the number of characters that appear before' \ 0 'in the string (excluding' \ 0 ').

The string pointed to by the parameter must end with '\ 0'.

Note that the return value of the function is size_t. Is unsigned.

#include<stdio.h> 

int main()
{
 const char*str1 = "abcdef";
 const char*str2 = "bbb";
 if(strlen(str2)-strlen(str1)>0)
 {
 printf("str2>str1\n");
 } 
 else
 {
 printf("srt1>str2\n");
 }
 return 0;
}

strcpy

char* strcpy(char * destination, const char * source );

The source string must end with '\ 0'.

The '\ 0' in the source string will be copied to the destination space.

The destination space must be large enough to hold the source string.

The target space must be variable.

strcat

char * strcat ( char * destination, const char * source );

The source string must end with '\ 0'.

The target space must be large enough to accommodate the contents of the source string.

The target space must be modifiable.

String cannot be appended to itself.

strcmp

int strcmp ( const char * str1, const char * str2 );

If the first string is greater than the second string, a number greater than 0 is returned.

If the first string is equal to the second string, 0 is returned.

If the first string is less than the second string, a number less than 0 is returned.

strncpy

char * strncpy ( char * destination, const char * source, size_t num );

Copy num characters from the source string to the destination space.

If the length of the source string is less than num, after copying the source string, append 0 to num after the destination.

strncat

char * strncat ( char * destination, const char * source, size_t num );
/* strncat example */
#include <string.h>
#include <stdio.h>

int main ()
{
 char str1[20];
 char str2[20];
 strcpy (str1,"To be ");
 strcpy (str2,"or not to be");
 strncat (str1, str2, 6);
 puts (str1);
 return 0;
}

strncmp

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

The comparison shows that another character is different, or a string ends, or num characters are all compared.

/* strncmp example */

#include <string.h>

#include <stdio.h>


int main ()

{

  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };

  int n;

  puts ("Looking for R2 astromech droids...");

  for (n=0 ; n<3 ; n++)

  if (strncmp (str[n],"R2xx",2) == 0)

 {

    printf ("found %s\n",str[n]);

 }

  return 0;

}

strstr

char * strstr ( const char *str1, const char * str2);
/* strstr example */

#include <string.h>

#include <stdio.h>


int main ()

{

  char str[] ="This is a simple string";

  char * pch;

  pch = strstr (str,"simple");

  strncpy (pch,"sample",6);

  puts (str);

  return 0;

} 

strtok

char * strtok ( char * str, const char * sep );

The sep parameter is a string that defines the set of characters used as delimiters

The first parameter specifies a string that contains 0 or more tags separated by one or more separators in the sep string.

The strtok function finds the next tag in str, ends it with \ 0, and returns a pointer to this tag. (Note: the strtok function will change the string to be manipulated, so the string segmented by the strtok function is generally a temporary copy of the content and can be modified.)

The first parameter of the strtok function is not NULL. The function will find the first tag in str, and the strtok function will save its position in the string.

The first parameter of strtok function is NULL. The function will start at the position saved in the same string to find the next tag.

A NULL pointer is returned if there are no more tags in the string.

/* strtok example */
#include <string.h>
#include <stdio.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
 {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
 }
  return 0;
}
#include <stdio.h> 


int main()

{

   char *p = "zhangpengwei@bitedu.tech";

 const char* sep = ".@";

 char arr[30];

 char *str = NULL;

 strcpy(arr, p);//Copy the data and process the contents of the arr array

 for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep))

 {

 printf("%s\n", str);

 }

}

strerror

char * strerror ( int errnum );

Return the error code and the corresponding error information.

/* strerror example : error list */

#include <string.h>

#include <stdio.h>

#include <srrno. h> / / header files that must be included

int main ()

{

  FILE * pFile;

  pFile = fopen ("unexist.ent","r");

  if (pFile == NULL)

    printf ("Error opening file unexist.ent: %s\n",strerror(errno));

    //errno: Last error number

  return 0;

}

Edit & Run

Simulation Implementation of library function

Simulated implementation of strlen

Mode 1:

//Counter mode
int my_strlen(const char * str)
{
 int count = 0;
 while(*str)
 {
 count++;
 str++;
 }
 return count;
}

Mode 2:

//Cannot create temporary variable counter

int my_strlen(const char * str)

{

 if(*str == '\0')

 return 0;

 else

 return 1+my_strlen(str+1);

}

Mode 3:

//Pointer - pointer mode

int my_strlen(char *s)

{

       char *p = s;

       while(*p != '\0' )

              p++;

       return p-s;

}

Simulation implementation strcpy

//1. Parameter sequence
//2. Function of function, stop condition
//3.assert
//4.const modifier pointer
//5. Function return value
//6. The title comes from the final part of the book "high quality C/C + + Programming"
char *my_strcpy(char *dest, const char*src)
{ 
 char *ret = dest;
 assert(dest != NULL);
 assert(src != NULL);
 
 while((*dest++ = *src++))
 {
 ;
 }
 return ret;
}

Simulated implementation of strcat

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

Simulation implementation str

char *  strstr (const char * str1, const char * str2)
{
        char *cp = (char *) str1;
        char *s1, *s2;
        if ( !*str2 )
            return((char *)str1);
        while (*cp)
       {
                s1 = cp;
                s2 = (char *) str2;
                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;
                if (!*s2)
                        return(cp);
                cp++;
       }
        return(NULL);
}        

Simulated implementation of strcmp

int my_strcmp (const char * src, const char * dst)

{

        int ret = 0 ;

        assert(src != NULL);

        assert(dest != NULL);

        while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)

                ++src, ++dst;

        if ( ret < 0 )

                ret = -1 ;

        else if ( ret > 0 )

                ret = 1 ;

        return( ret );

}

Keywords: C

Added by phppssh on Sun, 13 Feb 2022 14:30:23 +0200