[C language] Self-implementation of commonly used string functions and related functions

table of Contents

1. Commonly used library functions

1.strlen() function

2.strcpy() function

3.strcmp() function

         4.strcat() function

5. strchr() function

6. strrchr() function

7. strstr() function

Second, self-implementation of commonly used string functions


1. Commonly used library functions

1.strlen() function

  • Used to find the length of the string.

The strlen() function counts backward from the beginning of the string until it meets'\0', and then returns the value of the timer. The final counted string length does not include'\0'.

  • Header file: string.h
  • Grammar/prototype:

size_t strlen(const char* str);

  • Parameters: str is a pointer to a string.
  • Return value: the length of the string str (actually the number of valid bytes in the string).
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main() {
	char str[] = { "abcd" };
	int len1=strlen(str);
	printf("字符串长度(有效字符长度):length=%d\n", len1);
}

operation result:

2.strcpy() function

  • Used to copy (copy) a character string.
  • Header file: string.h
  • Grammar/prototype:

char* strcpy(char* strDestination, const char* strSource);

Parameter description: strDestination: destination string; strSource: source string.
strcpy() will copy the string pointed to by strSource to strDestination.
It must be ensured that strDestination is large enough to accommodate strSource, otherwise it will cause an overflow error.

  • Return value: the destination string, that is, strDestination.
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main() {
	char dest[50] = { 0 };
	char src[50] = { "hello" };
	strcpy(dest, src);
	puts(dest);
	
}

operation result:

3.strcmp() function

  • Used to compare two strings (case sensitive).
  • Header file: string.h
  • Grammar/prototype:

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

The parameters str1 and str2 are the two strings involved in the comparison.
strcmp() will compare each character of str1 and str2 in turn according to the ASCII code, until there is no character, or the end of the string (meet \0) is reached .

  • return value:

If the return value is <0, it means that str1 is less than str2.

If the return value> 0, it means that str2 is less than str1.

If the return value = 0, it means that str1 is equal to str2.

  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main() {
	char str1[50] ="hello" ;
	char str2[50] = "world";
	int res=strcmp(str1, str2);

	if (res > 0) {
		printf("%s > %s\n",str1,str2);
	}
	else if(res==0){
		printf("%s = %s\n", str1, str2);
	}
	else {
		printf("%s < %s\n", str1, str2);
	}
}

operation result:

4.strcat() function

  • Used to concatenate (concatenate) two strings.
  • Header file: string.h
  • Grammar/prototype:

char*strcat(char* strDestination, const char* strSource);

Parameter description: strDestination: destination string; strSource: source string.
The strcat() function appends the string pointed to by strSource to the end of the string pointed to by strDestination, so you must ensure that strDestination has enough memory space to accommodate two strings, otherwise it will cause an overflow error.
Note: The 'at the end of strDestination \0'will be overwritten, and the' at the end of strSource \0'will be copied together, and the final string is only one' \0'.

  • Return value: pointer to strDestination.
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main() {
	char str1[101] = { "hello" };
	char str2[50] = { "world" };
	strcat(str1, str2);
	puts(str1);
}

operation result:

5. strchr() function

  • Used to find the first occurrence of a certain character in a given string.
  • Header file: string.h
  • Grammar/prototype:

char* strchr(const char* str, char c);

Parameter description: str: the character string to be searched; c: the character to be searched.
The strchr() function will retrieve each character in the string str in turn until it encounters the character c, or reaches the end of the string (met \0).

  • Return value: Return the position of the first occurrence of the character c in the string str, or NULL if the character c is not found.
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main(){
	char* str = "hello world";
	char c = 'l';
	char* p = strchr(str,c);
	if (p) {
		puts("Found");
		printf("字符%c第一次出现的位置(下标)为:%d\n",c,(p-str)/sizeof(char));
	}
	else {
		puts("Not found");
	}
	return 0;
} 

operation result:

6. strrchr() function

  • Used to find the last occurrence of a certain character in a given string.
  • Header file: string.h
  • Grammar/prototype:

char* strrchr(const char* str, char c);

Parameter description: str: the character string to be searched; c: the character to be searched.

  • Return value: Returns the position of the last occurrence of the character c in the string str, or NULL if the character c is not found.
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main(){
	char* str = "hello world";
	char c = 'l';
	char* p = strrchr(str,c);
	if (p) {
		puts("Found");
		printf("字符%c最后一次出现的位置(下标)为:%d\n",c,(p-str)/sizeof(char));
	}
	else {
		puts("Not found");
	}
	return 0;
} 

operation result:

7. strstr() function

  • Used to find a specific substring in a given string.
  • Header file: string.h
  • Grammar/prototype:

char *strstr(const char *haystack, const char *needle)

Parameter description: haystack: source string; needle: substring.

  • Return value: Return the position where the needle string first appears in haystack, or NULL if it is not found.
  • [Example demonstration]
#include <stdio.h>
#include <string.h>

int main(){
	char* str1 = "hello world";
	char* str2 = "or";
	char* p = strstr(str1,str2);
	if (p) {
		puts("Found");
		printf("字符%s第一次出现的位置(下标)为:%d\n",str2,(p-str1)/sizeof(char));
	}
	else {
		puts("Not found");
	}
	return 0;
} 

operation result:


Second, self-implementation of commonly used string functions

1. Find the length of the string

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

int Length(const char* str) {
	int len = 0;
	assert(str);
	while (*str != '\0') {
		len++;
		str++;
	}
	return len;
}

int main() {
	const char* str1 = "hello world";
	printf("字符串长度:%d\n", Length(str1));
}

operation result:

2. String copy function

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

char* Mystrcpy(char* dest, const char* src) {
	assert(dest);
	assert(src);
	char* p = dest;
	while (*src != '\0') {
		*dest = *src;
		dest++;
		src++;
	}
	*dest = '\0';
	return p;
}
int main() {
	char str1[1024] = "hello" ;
	const char* str2 = "world";
	printf("字符串str1替换为:%s\n", Mystrcpy(str1,str2));

operation result:

3. String concatenation function

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

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

int main() {
	char str1[1024] = "hello" ;
	const char* str2 = "world";
	printf("字符串str1连接为:%s\n", Mystrcat(str1, str2));
}

operation result:

4. String comparison function

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

int Mystrcmp(const char* dest, const char* src) {
	assert(dest);
	assert(src);
	while (*dest == *src) {
		if (*dest == '\0') {
			return 0;
		}
		dest++;
		src++;
	}
	return (*dest - *src);
}
int main() {
	const char* str1 = "hello";
	const char* str2 = "world";
	printf("%d\n", Mystrcmp(str1, str2));
}

operation result:

5. Find the first occurrence of the specified character in the string

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

char* Mystrrchr(const char* str, char c) {
	assert(str);
	char* p = (char*)str;
	while (*p != c) {
		if (*p == '\0') {
			return NULL;
		}
		p++;
	}
	return p;
}

int main() {
	const char* str1 = "hello";
	char ch = 'o';
	char* p= Mystrrchr(str1,ch);
	if (p == NULL) {
		printf("Not found");
	}
	else {
		printf("字符%c第一次出现位置(下标):%d\n",ch, (p-str1)/sizeof(char));
	}
	
}

operation result:

 

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109036821