C language - detailed explanation of string library functions (including the simulation implementation of some functions)

In the interview, you are often required to write some basic library functions, especially the string library functions. This paper summarizes some common string library functions.

Generally, to use these functions, you need to include a header file:

#include <string.h>

1, strlen

Function function:

A function for finding the length of a string.

Function prototype:

size_t strlen ( const char * str );

Function Description:

  • The string ends with '\ 0'. The strlen function returns the number of characters that appear before the string '\ 0' (excluding '\ 0')
  • The return value of the function is size_t. Is unsigned

The following is an example to explain the concept that the return value is unsigned:

#include <stdio.h>
#include <string.h>
int main()
{
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	if (strlen(str2) - strlen(str1) > 0)
	{
		printf("hello\n");
	}
	else
	{
		printf("hallo\n");
	}
	return 0;
}

The results are as follows:  

Because the return value of the strlen function is unsigned, the result of strlen(str2)-strlen(str1) is 3, so the output result is hello.

Simulation Implementation:

#include <stdio.h>
#include <assert.h>

int my_strlen(const char *p)
{
	assert(p); //Determine whether the pointer is empty
	int count = 0;
	while (*p != '\0') 
	{
		count++;
		p++;
	}
	return count;
}
int main()
{
	char arr[] = "abcdef";
	int ret = my_strlen(arr);
	printf("%d\n", ret);
	return 0;
}

2, strcpy

String copy function.

Function function:

Copy the source string to the destination space.

Function prototype:

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

Function Description:

  • The source string must end with '\ 0'
  • The destination space must be variable and large enough to hold the source string
  • When copying, '\ 0' in the source string will be copied to the target space  

Simulation Implementation:

#include <stdio.h>
#include <assert.h>

char * my_strcpy(char* dest, const char* src)
{
	assert(dest != NULL); //Determine whether the pointer is empty
	assert(src != NULL);
	char * ret = dest;
	while (*dest++ = *src++) //Copy the string pointed to by src to the space pointed to by dest
	{
		;
	}
	//Return to the starting position of destination space
	return ret;
}
int main()
{
	char arr1[] = "abcdef"; //The target space must be large enough
	char arr2[] = "bit";
	my_strcpy(arr1, arr2);
	printf("%s\n",arr1);
	return 0;
}

3, strcat

String append function.  

Function prototype:

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

Function Description:

  • The source string must end with '\ 0'
  • The destination space must be variable and large enough to hold the source string
  • When appending, it starts from '\ 0' in the target space

So, can a string be appended to itself? The answer is No. (can be implemented with strncat)

Because:

When you append to yourself, for example, the string "abcdef" is actually abcdef abcdef \ 0.. When you append to yourself, put a in the position of \ 0 and B, C, D, e and F in the back. Later, you find that \ 0 is gone and has been changed to A. there is no \ 0 (not to the binding position), so you can't stop, so the program will crash.

Simulation Implementation:  

#include <stdio.h>
#include <assert.h>

char * my_strcat(char* dest, const char* src)
{
	assert(dest);
	assert(src);
	char* ret = dest;
	while (*dest)  //Found '\ 0' in destination string
	{
		dest++; 
	}
	while (*dest++ = *src++) //Add
	{
		;
	}
	return ret;
}
int main()
{
	//Append string arr2 after array arr1
	char arr1[10] = "abc";
	char arr2[] = "bcd";
	printf("%s\n", my_strcat(arr1, arr2));
	return 0;
}

4, strcmp

String comparison function.

Function Description:

  • The parameter str1 and str2 strings are compared by comparing the ASCII codes of each character in the string. The case of the character is considered when comparing
  • String str1 is greater than str2 and returns a number greater than 0; The string str1 is equal to str2 and returns 0; String str1 is less than str2 and returns a number less than 0

Function prototype:

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

Simulation Implementation:

#include <stdio.h>
#include <assert.h>

int my_strcmp(const char* dest, const char* src)
{
	assert(dest != NULL);
	assert(src != NULL);
	while (*dest == *src)
	{
		if (*dest == '\0')
		{
			return 0; //Two strings are equal
		}
		dest++;
		src++;
	}
	if (*dest > *src)
	{
		return 1;//greater than
	}
	else
	{
		return -1;//less than
	}
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abd";
	int ret = my_strcmp(arr1, arr2);
	printf("ret=%d\n", ret);
	return 0;
}

  5, strstr

Find the first occurrence of str1 string in str2 string (excluding '\ 0' of str1).

Function prototype:

char * strstr ( const char *str2, const char * str1);
  • strstr   Returns a pointer to the position. If it is not found, it returns a null pointer.

  Simulation Implementation:

#include <stdio.h>
#include <assert.h>

char * my_strstr(const char* dest, const char* src)
{
	assert(dest); //Determine whether the pointer is empty
	assert(src);
	char* ret1 = dest; 
	char* ret2 = src;
	char* cur = dest;
	if (*src == '\0')
	{
		return dest; //If the string arr2 to be searched is empty, the first address of the string arr1 is returned
	}
	while (*dest)
	{
		ret1 = cur;
		ret2 = src;
		while ((*ret1 != '\0') && (*ret2 != '\0') && (*ret1 == *ret2))
		{
			ret1++;
			ret2++;
		}
		if (*dest == "\0")
		{
			return ret1;
		}
		cur++;
	}
	return NULL;
}
int main()
{
	char arr1[] = "abccdef";
	char arr2[] = "cde";
	char * ret = my_strstr(arr1, arr2); //Finds whether the arr2 string is in the arr1 string
	if (ret == NULL)
	{
		printf("Ӵ\n");
	}
	else
	{
		printf("%s\n",ret);
	}
	return 0;
}

6, strncpy  

Function prototype:

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

Function Description:

  • 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 ('\ 0') to the end of the target until num characters

Function implementation:

#include <stdio.h>
#include <string.h>
 
int main()
{
    char arr1[10] = "abcdef";
    char arr2[] = "bit";
    strncpy(arr1, arr2, 6);
    return(0);
}

7, strncat

Function prototype:

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

Function function:

Add the first n characters of the string indicated by source to the end of destination (overwrite '\ 0' at the end of dest) and add '\ 0'  .

Function Description:

  • The memory areas referred to by source and destination cannot overlap
  • destination must have enough space to hold the string of source

Function implementation:

#include <stdio.h>
#include <string.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;
}

8, strncmp

Function prototype:

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

Function function:

Compare the first num characters of the strings str1 and str2.

Function Description:

If the first num bytes are exactly equal, the return value is 0; During the comparison of the first num bytes, if * str1 and * str2 are different, it returns (* str1-*str2).

Function implementation:

#include <stdio.h>
#include <string.h>

int main()
{
	const char *p1 = "abczdef";
	char *p2 = "abcqwer";
	int ret = strncmp(p1, p2, 4);
	printf("%d\n", ret);
	return 0;
}

Tags: C C++ Interview

Posted on Mon, 20 Sep 2021 21:13:25 -0400 by Dasndan