[C Language] Summary of Array Knowledge (3) - String Processing Functions

Preface

There are no operators for string assignment, merging, and comparison in C language, but some standard functions for processing strings are provided. When calling the string processing function, the following preprocessing header file must be included at the beginning of the program:

#include<string.h> 

1. Character copy function strcpy()


  • General format: strcpy (string 1, string 2)
  • Function: Copy string 2 to string 1
  • Parameter string 2 can be a character array name or a string constant. But parameter string 1 must be a character array name
  • String 1 must have enough space to hold String 2
  • Copy until '\0' is encountered in string 2 (including '\0')

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[]="123456789";
	strcpy(c1+4,"hello");//把字符串"hello"复制到c1+4开始的位置
	puts(c1);
	return 0;
}

result:

1234hello   //'\0'将9替换了

  • General format: strncpy (string 1, string 2, n)
  • Function: Copy the first n characters of string 2 to string 1

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char str1[10]="abcdefg",str2[]="ABCDEF";
	strncpy(str1,str2,4);
	puts(str1);
	return 0;
}

result:

ABCDefg

2. String concatenation function strcat()


  • General format: strcat(String 1, String 2)
  • Function: Concatenate two strings
  • Parameter string 2 can be a character array name or a string constant. But parameter string 1 must be a character array name
  • String 1 must have enough space to hold String 2
  • When connecting, the first character of string 2 is used to overwrite the end mark '\0' of string 1, and the system adds an end mark '\0' to the end of the new string.

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[20]="hello,";
	strcat(c1,"how are you!");
	puts(c1);
	return 0;
}

result:

hello,how are you!

3. String comparison function strcmp()


  • General format: strcmp (string 1, string 2)
  • Function: Compare the size (not length) of two strings
  • String comparison rules: Starting from the first characters of the two strings, compare the ASCII codes of the corresponding characters in sequence until different characters or '\0' appear. If all characters are the same, return 0; otherwise, based on the comparison result of the first different character, return the difference between the two characters (string 1 minus string 2)
  • The function return value is an integer:
    1. When string 1 < string 2, the return value < 0
    2. When string 1 = string 2, the return value = 0
    3. When string 1 > string 2 , return value>0

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[] = "12345", c2[] = "12345";
	printf("%d\n", strcmp(c1, c2));
	printf("%d\n", strcmp("zhang", "zhap"));
	printf("%d\n", strcmp("zhap", "zhang"));
	return 0;
}

result:

0
-1 //n<p
1

Why 1 instead of 2?


It may be related to the specific compiler. I am using VS2022 here. (I will talk about it in detail next time)


4. Find the string length function strlen()


  • General format: strlen (string)
  • Function: Calculate the length of the string, excluding the end mark '\0'

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[] = "12345";
	printf("%d,%d\n", strlen(c1),sizeof(c1));
	printf("%d,%d\n", strlen("12345"), sizeof("12345"));
	return 0;
}

result:

5,6
5,6

5. Convert uppercase letters to lowercase letters function strlwr()


  • General format: strlwr (string)
  • Function: Convert uppercase letters to lowercase letters

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[] = "AaBbCc";
	printf("%s\n", c1);
	printf("%s\n", strlwr(c1));
	return 0;
}

result:

AaBbCc
aabbcc

6. Convert lowercase letters to uppercase letters function strupr()


  • General format: strupr (string)
  • Function: Convert lowercase letters to uppercase letters

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char c1[] = "AaBbCc";
	printf("%s\n", c1);
	printf("%s\n", strupr(c1));
	return 0;
}

result:

AaBbCc
AABBCC

Guess you like

Origin blog.csdn.net/m0_74102736/article/details/130330355