The secret of string function

catalogue

1. String function

1.strcpy application and Implementation

 2. Application and implementation of strcat

 3. Application and implementation of StrCmp

4. STR application and Implementation

1. String function

When it comes to string functions, I believe they are not unfamiliar. Today, we mainly understand the application and implementation of strcpy, strcat, strcmp and strstr.

1.strcpy application and Implementation

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

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

This function copies the string content in "source" to destination. If there is content in destination, it will be overwritten. The code is shown below.

(please remember to use the header file < string. H >)

So how do you implement this function,

Referring to the definition of the function, the return value is the first address of destination, and then through a for loop until it encounters' \ 0 '. Of course, the loop condition can also be strlen (* s).

 2. Application and implementation of strcat

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

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

The function of "destination" is not the same as that of "destination" in the original string. The code is shown below

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

int main(){
    char s1[]="hello";
    char s2[]="world";
    strcat(s1,s2);
    printf("%s",s1);
}

output:helloworld

The implementation of this function is also very simple. We just need to delete the '\ 0' in the destination and move the source string to the destination (note that we must ensure that there is enough space in the destination). The following is the code implementation.

 

 

 3. Application and implementation of StrCmp

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

Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached

As the name suggests, this function is to compare two strings, but this comparison is not a general comparison. Let's learn about this function:

  1. Returns 0 when the strings are equal in length and content.
  2. When the corresponding bits of the string are different, compare the ascii code value. If STR1 > STR2, a positive value is returned; otherwise, a negative value is returned
  3. When str2 is a substring of str1, a negative value is returned

How to implement it? The code is as follows:

#include<stdio.h>

int my_strcmp(char* s1, char* s2) {
	for (int i = 0; *(s1 + i) != '\0' && *(s2 + i) != '\0'; i++) {
		if (*(s1 + i) > *(s2 + i)) {
			return 1;
		}
		else if (*(s1 + i) < *(s2 + i)) {
			return -1;
		}
	}
}

int main() {
	char s1[] = "ABCD";
	char s2[] = "CDEF";
	int ret = my_strcmp(s1, s2);
	printf("%d", ret);
}

4. STR application and Implementation

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

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters, but it stops there. 

This function is the substring str2 found in str1. If found, it returns str2. If not found, it returns null.

(this is mainly the flexible application in the topic)

The implementation code is as follows

#include<stdio.h>

char* my_strstr(const char* s1, const char* s2) {
	int i = 0;
	int j;
	for (j = 0; *(s1 + j) != '\0'; j++) {
		if (*(s1 + j) == *(s2 + i)) {
			break;
		}
	}
	for (i = 0; *(s2 + i) != '\0' && *(s1 + j) != '\0'; i++) {
		if (*(s2 + i) == *(s1 + j)) {
			continue;
		}
		else {
			return NULL;
		}
	}
	return s2;
}

int main() {
	char* ptr;
	char s1[] = "i love ttt";
	char s2[] = "ta";
	ptr = my_strstr(s1, s2);
	if (ptr == NULL) {
		printf("NOT FUNDED");
	}
	else {
		printf("FONDED");
	}
}

Keywords: C linq

Added by xeirus on Sat, 05 Feb 2022 09:58:06 +0200