C/C + + string function

Posted by andysez on Sat, 25 Dec 2021 22:34:03 +0100

unrestricted

Length of string

strlen(const char* s)
Returns the string length until '\ 0' is encountered in s

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>

int main()
{
	char str[10] = "Hello";
	int len = strlen(str);
	printf("%d\n", len);//The calculated length is 5, and '\ 0' will not be calculated
	//sizeof() will add '\ 0'
	int sz = sizeof(str);
	printf("%d\n", sz);//Find the total size of the array, including '\ 0'
	return 0;
}

string copy

String copy (copy all, unlimited)

strcpy(char* des , const char* src)
Copy the contents of src to des. If there is data in DES, it will be completely overwritten

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
	char s[10] = "hello";
	char* s2 = "world";
	printf("Original data:%s\n", s);
	strcpy(s, s2);
	printf("Copied data:s = %s\n", s);
	return 0;
}

Note: the length space of the target string should be greater than that of the original string.

String append

String append (all appended, unrestricted) strcat(char* des,const char* src)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>

int main()
{
	char str[15] = "hello";
	char* sr = "world";
	strcat(str, sr);
	printf("After addition:%s\n", str);
	return 0;
}


Note: the target string length space should be larger than the original string length space
sizeof(str)>sizeof(sr)

String comparison

String comparison (all original strings are compared without restriction)
strcmp(char* des,const char* src)

=0 equal
0 the string des is greater than the string src
< 0 des is less than src

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
//Compare the results with three different values
	char s1[10] = "Hello";
	char s2[10] = "Worldhhh";
	char s3[10] = "A";
	char s4[10] = "Hello";
	int l1 = strcmp(s1, s2);//less than
	printf("l1 = %d\n", l1);
	int l2 = strcmp(s1, s3);//greater than
	printf("l2 = %d\n", l2);
	int l3 = strcmp(s1, s4);//equal
	printf("l3 = %d\n", l3);
	return 0;
}

characters finding

strchr(char* des,int c)
Find the character src in the string des and return the first occurrence when it is found
src address, return NULL pointer not found

int main()
{
	char s[10] = "helda";
	char s2 = 'e';
	printf("e First address:%d\n", &s[1]);
	char* sz = strchr(s, s2);
	printf("sz Return to first address = %d", sz);
}

The character e is found in s, and the address of E is returned
Operation results:

Restricted

String copy (restricted, specified length copy)

strncpy(char* des, const char* src,unsigned int size)
des is the string to copy to
src is the first address of the source string to be copied, and size is the number of characters to be copied

int main(){
	char s[10] = "hello";
	char s2[10] = { 0 };
	strncpy(s2, s, 4);
	printf("s2 = %s", s2);
}

I only intercepted the first four digits, so it shows shell

String append (restricted, specified length append)

strncat(char* des, const char* src,unsigned int size)
des is the string to append to
src is the first address of the source string to be appended, and size is the number of characters to be appended

int mian()
{
	char s[10] = "hello";
	char s2[10] = "wo";
	strncat(s, s2, 1);
	printf("s = %s", s);
}

I only appended one character, so hello only appended one character
Operation results:

String comparison (restricted, specified length comparison)

strncmp(char* des, const char* src,unsigned int size)
des is the destination string to compare
src the source string to be compared is the first address, and size refers to the number of characters to be compared

int main()
{
	char s[10] = "hello";
	char s2[10] = "helda";
	int ret = strncmp(s, s2, 3);
	printf("ret = %d", ret);
}

I only compared the first three characters, so the returned 0 is equal
Operation results:

String lookup (restricted, specified length lookup)

strstr(char* des,const char* src)
Find src in the string des. If it is found, it returns the first address of the src that appears for the first time. If it is not found, it returns a NULL pointer

int main()
{
	char s[10] = "helda";
	char s2[10] = "helzs";
	printf("s2 First address:%d\n", s2);
	char* sz = strstr(s, s2);
	printf("sz Return to first address = %d",sz );
}

Return null pointer not found
Operation results:

String segmentation

strtok(char* des,const char* src)
If src is found in the string des, the string before src is returned
After that, strtok(NULL,src) continues to search src. If it finds the src, it returns the string from the last found src to the src found this time. Until it cannot be found, it will return a null pointer.

int main()
{
	char s[10] = "hel";
	char s2[10] = "helsada";
	char * res = strtok(s2, s);
	printf("res = %#x\n", res);
	char* ret = strtok(NULL, s);
	printf("ret = %d", ret);
}

The segment s is intercepted in s2, and s2 finds the first address of the string before s
Operation results

Memory operation function

Memory Copy

void *memcpy(void dest, const void src, size_t n)
We can see that both str1 and str2 are void, and the return type is also void, so it can copy memory including custom types
n indicates the length of the copy

int main(){
	char s[10] = "hello";
	char s2[10];
	memcpy(s2, s, 3);
	printf("s2 = %s", s2);
}

I only copied 3 characters, so s2 only has hel
Operation results:

Memory move override

void * memmove ( void * destination, const void * source, size_t num )
Move the character of num length from source to destination, and overwrite it from the first address of destination
Return to the first address after moving

int main(){
	char s[10] = "hello";
	char s2[10] = "why";
	memmove(s, s2, 2);
	printf("s = %s", s);
}

I moved the first two characters in s2 to s and overwritten the first two characters in S.
Operation results:

Memory settings (commonly used for initialization)

void * memset(void* str,int c,size_t n)
Set the contents of the first n characters of str to c ---- we often use it for initialization
Return to the set first address

int main(){
	char s[10] = "hello";
	char s2[10];
	memset(s2, 'a', sizeof(char) * 10);
	for (int i = 0; i < sizeof(char)*10; i++)
	{
		printf("s2 = %c\n", *(s2 + i));
	}
}

I initialize the string s2 to 'a'
Operation results:

Memory comparison

void* memcmp(const void* ptr1,const char* ptr2,size_t num)
Compare the first num bytes of ptr2 with ptr1
PTR1 > ptr2 returns a value greater than zero
ptr1 = ptr2 returns zero
PTR1 < ptr2 returns a value less than zero

int main(){
	char s[10] = "hello";
	char s2[10] = "hels";
	int ret1 = memcmp(s, s2, 3);
	int ret2 = memcmp(s, s2, 4);
	printf("ret1 = %d\n", ret1);
	printf("ret2 = %d", ret2);
}

The first time I compare the first three characters, equal returns 0
The second time I compare the first four characters, s is smaller than s2 and returns - 1

Topics: C C++