Summary of commonly used string functions---1

/************************************************* ************************************************** **/
Commonly used string functions are described in the following brain map. For a complete list of string.h series functions, please refer to "New Standard ANSI C Library for C99 and C11"
/********* ************************************************** *****************************************/

【image】

strlen() function

The following shows a test fit () function, pay attention to the use of C character constant series characteristics

#include <stdio.h>
#include <string.h>  /*内含字符串函数原型*/
void fit(char *,unsigned int);
int main (void){
    
    
    char mesg [] = "Things should be as simple as possible,""but not simper.";
    puts(mesg);
    fit(mesg,38);
    puts(mesg);
    puts("Let' LOOk at some more of the string");
    puts(mesg+30);
    return 520;
}
void fit(char *string,unsigned int size){
    
    
    if(strlen(string)>size)
        string[size]='\0';
}

The output is as follows

Insert picture description hereThe fit() function replaces the comma of the 39th element with the character'\0', and the puts() function stops the output at the empty character and ignores the remaining characters.
Insert picture description here

strcat() function

Since the previous brain map has explained the usage of this function, then we directly go to the code

#include "stdio.h"
#include "string.h"

#define size 80

int main(){
    
    
	char flower[size]="woaini";
	char addon[50]="lala semll like older";
	strcat(flower ,addon);
	puts(flower);
	puts(addon);
	
	return 520;
} 

The result is as follows. It
Insert picture description here
can be seen that the flower has changed, but the addon remains unchanged.

But what if the size of the flower array is not big enough? Will there be problems again? If you have such concerns, it means you are no longer a noob. Then let us continue to look down!

strncat() function

The strcat() function cannot check whether the first array can hold the second string.

strncat(char *,char *,int)

The third parameter n means append the content of the second string to the first string until the nth character

#include "stdio.h"
#include "string.h"

#define size 80

int main(){
    
    
	char flower[size]="woaini";
	char addon[50]="lala semll like older";
	strncat(flower ,addon,10);
	puts(flower);
	puts(addon);
	
	return 520;
} 

Results of the

Insert picture description here

strcmp() function

1. This function is used for string comparison

At the same time, it is worth mentioning that this function compares the content of the string, not the address of the string. Readers can design a function by themselves, or use the strcmp() function in the C standard library (for string comparison). This function uses comparison operators to compare strings, just like comparing numbers. If the two string parameters are the same, the function returns 0, otherwise it returns a non-zero value.

#include "stdio.h"
#include "string.h"

#define size 80

int main(){
    
    
	char flower[size]="woaini";
	char addon[50]="woaini";
	char *a="wobuaini";
	printf("%d\n",strcmp(flower,addon));
	printf("%d\n",strcmp(flower,a));
	return 520;
} 

The results of the implementation are as follows: a
Insert picture description here
careful friend may ask, is the size of the flower array not 80 characters? Our answer to this is that the strcmp() function compares only strings, not the entire array, which is very good Function.

2. The return value of the strcmp() function

#include "stdio.h"
#include "string.h"

int main(){
    
    
	printf("strcmp(\"A\",\"A\") is");
	printf("   %d\n",strcmp("A","A"));
	
	printf("strcmp(\"A\",\"B\") is");
	printf("   %d\n",strcmp("A","B"));
	
	printf("strcmp(\"B\",\"A\") is");
	printf("   %d\n",strcmp("B","A"));
	
	return 520;
} 

Insert picture description here
The above situation shows that the comparison of strings is based on the ASCII code of the first different character of the two strings to determine the output value. There are three situations:

  • Two strings are equal, return 0
  • If the first string is before the second string in the alphabet, return a negative number
  • Otherwise return a positive number

strncmp() function

The strncmp() function, as the name suggests, is an extension of the strcmp function. The strcmp() function compares characters in a string until a different character is found. This process may continue to the end of the string. When the strncmp() function compares two strings, it can compare the characters that are different, or only compare the number of characters specified by the third parameter. For example, to find a string starting with "astro", you can limit the function to only find these 5 characters.

Since the operation is similar to the strcmp() function, actual combat will not be launched.

strcpy() function and strncpy() function

The use of strcpy() function can only be realized, the entire string copy of the string.

1. Other attributes of strcpy()

  • The return type of strcpy() is char*, which returns the value of the first parameter, which is the address of the first character
  • The first parameter does not necessarily point to the beginning of the array, this attribute can be used to copy part of the array
#include "stdio.h"
#include "string.h"
#define words "beast"
#define size 40

int main(){
    
    
	
	const char *orig=words;
	char copy[size]="be the best that you can";
	char *ps;
	
	puts(orig);
	puts(copy);
	ps=strcpy(copy+7,orig);
	puts(copy);
	puts(ps);
	
	return 520;
}

Output result
Insert picture description here

As you can see, the value pointed to by the ps pointer is beast, which is the address of the first parameter, which is copy+7. Have you got it yet? If it is not easy to understand, please see the picture below

Insert picture description here2. A more cautious choice: strncpy()

Both strcpy() and strcat() have the same problem, neither of them can check whether the target space can hold a copy of the string. Therefore, it is safer to copy strings using strncpy(). Similarly, the third parameter of the function specifies the maximum number of characters that can be copied.

Supplement: sprintf() function

First of all, the sprintf() function is declared in stdio.h, not string.h.
Next, use a string of codes to illustrate his usage.

#include "stdio.h"
#include "string.h"
#define words "beast"
#define size 40

int main(){
    
    
	char formal[2*size+10];
	const char *orig=words;
	char copy[size]="be the best that you can";
	sprintf(formal,"  %-19s:  %6.2f\n",orig,copy);
	puts(formal);
	
	return 520;
}

Results of the

Insert picture description here
If you feel helpful, please give a thumbs up and leave a comment before leaving, thank you dear.

Guess you like

Origin blog.csdn.net/qq_42392049/article/details/112667648