当前位置:网站首页>String search in C

String search in C

2022-06-10 22:12:00 sinat_ forty-one million seven hundred and fifty-two thousand t

Catalog

1. Find single character strchr/strrchr

2. Find any of several characters  strpbrk

3. Find a substring strstr

4. Check two strings one at a time strspc/strcspn


String lookup is divided into : Find a single character in a string , Find multiple characters in a string , Find substring in string .

Find a single character using strchr And strrchr;
Find multiple characters using strpbrk;
Find substrings using strstr;
strspn and strcspn, Check whether the two strings are the same one by one

Like other string handlers , You need to include header files when using these functions <string.h>

1. Find single character strchr/strrchr

strchr And strrchr The function is declared as follows :

char *strchr(const char *s, int c);
char *strrchr(const char *s, intc);

Both of these functions are derived from the string s Search for characters c, If you find a character c Just go back to , The return value points to this location , If no characters are found c, return NULL.

strchr And strrchr The difference between functions is :strchr Find characters from left to right ,strrchr Find characters from right to left .

Examples of use , In string str Search for characters c And print the results :

	char *str = "When we let go of something.it opens up a little space to grow.";
	int c = 'g';
	char *ppos = strchr(str,c);
	char *ppos1 = strrchr(str,c);

	printf("str\t\t= %s\n",str);
	printf("strchr ret\t= %s\n",ppos);
	printf("strrchr ret\t= %s\n",ppos1);

The output is as follows :

str                = When we let go of something.it opens up a little space to grow.
strchr ret     = go of something.it opens up a little space to grow.
strrchr ret    = grow.

2. Find any of several characters  strpbrk

strpbrk Function declaration :

char *strpbrk(const char *s1, const char *s2);

strpbrk Function in the source string s1 Find the first string in the sequence from front to back s2 The position of any character in and returns , It doesn't contain '\0', If a null pointer is not found . The function is defined as follows :

char *strpbrk(const char *s1, const char *s2)
{
	while( *s1 != '\0')
	{
		const char *a = s2;
		while(*a != 0)
		{
			if(*a++ == *s1)
				return (char*)s1;
			++s1;
		}
	}
}

Examples of use :

	char *str = "When we let go of something.it opens up a little space to grow.";
	char* str2 = "met";
	char *ppos2 = strpbrk(str,str2);
	printf("str\t\t= %s\n",str);
	printf("strpbrk ret\t= %s\n",ppos2);

Output results :

str             = When we let go of something.it opens up a little space to grow.
strpbrk ret     = en we let go of something.it opens up a little space to grow.

3. Find a substring strstr

strstr Function declaration :

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

strstr Function in string str1 Find from left to right in str2,str1 Continuous inclusion str2 All characters in the , return str1 For the first time str2 A pointer to the position of , If not found , return NULL.

Examples of use :

	char *str = "When we let go of something.it opens up a little space to grow.";
	char* str2 = "met";
	char *ppos3 = strstr(str,str2);
	printf("str\t\t= %s\n",str);
	printf("strstr ret\t= %s\n",ppos3);

Output results :

str             = When we let go of something.it opens up a little space to grow.
strstr ret    = mething.it opens up a little space to grow.

4. Check two strings one at a time strspc/strcspn

strspn and strcspn Function declaration :

size_t strspn(const char *s,const char *accept);
size_t strcspn(const char *s,cosnt char *reject);

strspn From a string s The first character of , Check and string one by one accept Whether the characters in inequality , If it's not the same , Stop checking , Returns as a string s The beginning contains accept The number of characters in the ;

strcspn From a string s The first character of , Check and one by one reject Whether the characters in identical , If equal , Stop checking , Returns as a string s The beginning is continuous without a string reject Number of characters in ,

Examples of use :

	char *str = "When we let go of something.it opens up a little space to grow.";
char *str3 = "bdjk";
	size_t pos1 = strspn(str,str3);
	size_t pos2 = strcspn(str,str3);

	printf("%s = %d\n",str3,pos1);
	printf("%s = %d\n",str3,pos2);

	char *str4 = "When";

	pos1 = strspn(str,str4);
	pos2 = strcspn(str,str4);

	printf("%s = %d\n",str4,pos1);
	printf("%s = %d\n",str4,pos2);

Output results :

str             = When we let go of something.it opens up a little space to grow.
bdjk = 0
bdjk = 63
When = 4
When = 0

Use strspn and strcspn Need to pay attention to :

        Use strspn when , When the first character is different or the string s It doesn't contain accept When any character in , return 0;
        Use strcspn when , When the first character is the same , return 0; When string s It doesn't contain accept Any character of , Return string length .

Reference resources : String lookup function ,C Language string lookup function details (biancheng.net)

原网站

版权声明
本文为[sinat_ forty-one million seven hundred and fifty-two thousand t]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102049288429.html