The C language implements instances of string manipulation functions

  • 2020-05-27 06:45:02
  • OfStack

The C language implements instances of string manipulation functions

In the process of writing the program, we often use some string functions, such as find the length of the string, copy the string... , these functions all exist in the C standard library and we can use them directly. But we also need to master the implementation of these functions, today we will take a look at some common string manipulation function implementation methods.

1.strlen

strlen is a function that calculates the length of a string, which is the number of characters it contains.

Today I'm going to show you three ways to implement the strlen function

(1) define a counter count


// way 1 Definition: 1 A counter 
size_t my_strlen(const char *str)
{
  int count = 0;   
  while (*str)
  {
    count++;     
    str++;
  }
  return count;
}

(2) recursive implementation


size_t my_strlen(const char *str)
{
  if (*str == '\0')
    return 0;
  else
    return my_strlen(str + 1) + 1;
}

(3) use pointer - pointer


size_t my_strlen(const char *str)
{
  const char *end = str;
  while (*end++)
  {
    ;
  }
  return end - str - 1;
}

2.strcpy

The function used to copy the string is strcpy, and its prototype is as follows:


char *strcpy(char *dest, const char *src);

There are a few things to note when using this function

(1) the space of the target character array must be large enough to accommodate the strings to be copied
(2) the target character array can be modified
(3) the copied string should be able to find '\0'


char *my_strcpy(char *dest, const char *src)
{
  char *tmp = dest;
  assert(dest);
  assert(src);
  while (*dest++ = *src++)
  {
    ;
  }
  return tmp;
}

3.strcat

The strcat function allows you to add (concatenate) one string to the end of another. The strcat function requires that the dest parameter already contains a string (which can be empty). It finds the end of the string and adds a copy of the src string to this location.


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

4.strcmp

strcmp is used to compare two strings and compare the corresponding characters one by one until a mismatch is found. The string in which the "smaller" of the first mismatched character is said to be "less" than the other string. If one of the strings is the first part of the other string, it is also considered "less" than the other string because its '\0' appears earlier. Note that the strcmp function compares strings that contain only uppercase letters or lowercase letters.


int my_strcmp(const char *src1, const char *src2)
{
  while (*src1 == *src2)
  {
    if (*src1 == '\0')
      return 0;
    src1++;
    src2++;
  }
  return *src1 - *src2;
}

5.strstr

To find a substring in a string, you can use the strstr function, which looks for the first occurrence of the entire s2 in s1 and returns a pointer to that location. If s2 does not appear anywhere in s1, the function returns an NULL pointer. If the second function is an empty string, the function returns s1.


char *my_strstr(const char* s1, const char* s2)
{
  const char *p = s1;
  const char *q = s2;
  const char *cur = NULL;
  assert(s1);
  assert(s2);
  if (*s2 == '\0')
    return s1;
  while (*p)
  {
    cur = p;
    while ((*p) && (*q) && (*p == *q))
    {
      p++;
      q++;
    }
    if (*q == '\0')
      return cur;
    p = cur + 1;
    q = s2;
  }
  return NULL;
}

6.strchr

strchr is used to look up a specific character. The first occurrence of the character ch is found in the string str. After finding the location, the function returns a pointer to that location. If the character does not exist in the string, the function returns an NULL pointer.


char *my_strchr(char const *str, int ch)
{
  const char *tmp = str;
  while (*tmp)
  {
    if (*tmp == ch)
    {
      return tmp;
    }
    tmp++;
  }
  return NULL;
}

7.strrchr

Another lookup function similar to strchr is strrchr, which differs from strchr in that it returns a pointer to the last occurrence of the character in the string


char* my_strrchr(const char* str, int ch)
{
  char* pos = 0;
  assert(str);
  while (*str)
  {
    if (*str == ch)
    {
      pos = str;
    }
    str++;
  }
  if (pos != 0)
  {
    return pos;
  }
  else
    return NULL;
}

A string function whose length is limited

The standard library also contains functions that handle strings in a different way. These functions take a length parameter that is displayed to limit the number of characters to copy or compare.

1.strncpy

Like strcpy1, strncpy copies the characters of the source string to the target space, but it always copies len characters to dest exactly. If the value of strlen(src) is less than len, the dest array is filled with an extra '\0' to the len byte length. If the value of strlen(src) is greater than or equal to len, then only len characters are copied to dest.


size_t my_strlen(const char *str)
{
  if (*str == '\0')
    return 0;
  else
    return my_strlen(str + 1) + 1;
}
0

2.strncat

strncat, which copies up to len characters from src to the end of the target array.


size_t my_strlen(const char *str)
{
  if (*str == '\0')
    return 0;
  else
    return my_strlen(str + 1) + 1;
}
1

3.strncmp

strncmp is also used to compare two strings, but it compares len bytes at most. If two strings have unequal characters before the len character, this function stops the comparison like strcmp1 and returns the result. If the first len characters of two strings are equal, the function returns zero.


size_t my_strlen(const char *str)
{
  if (*str == '\0')
    return 0;
  else
    return my_strlen(str + 1) + 1;
}
2

There are many more string functions in the standard library, so that's all for today.

The above is the operation of C language string summary, if you have any questions, please leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!


Related articles: