Article directory
foreword
Summary of the commonly used string processing functions strlen function, strcpy function, strcat function and strcmp function in c language.
1. Use of strlen function
1. use
The strlen function returns the number in the string, but does not include the string terminator '\0'
#include<stdio.h>
#include<string.h>
int main()
{
char str1[] = "abcdef";
printf("%ld\n", strlen(str1));
return 0;
}
执行
```cpp
# gcc strlen.c -o strlen
./strlen
6
2. Implementation method
#include<stdio.h>
#include<string.h>
size_t Strlen_char(const char* str1)
{
size_t len = 0;
while (*str1 != 0)
{
len++;
str1++;
}
return len;
}
int main()
{
char* str1 = "abcdef";
printf("%ld\n", Strlen_char(str1));
return 0;
}
implement
# gcc strlen.c -o strlen
./strlen
6
Two, strcpy function use
1. Use
prototype:
char *strcpy(char * destination, const char *source );
strcpy is an overlay copy, copying the full coverage of the source to the destination will also copy '\0', the space of the estination must be >= the space of the source
#include<stdio.h>
#include<string.h>
int main()
{
char p1[] = "abcde";
char* p2 = "hello";
strcpy(p1, p2);
printf("%s\n", p1);
printf("%s\n", p2);
return 0;
}
implement
# gcc strcpy.c -o strcpy
# ./strcpy
hello
hello
2. Implementation method
#include<stdio.h>
#include<string.h>
void stringcpy(char* char1 , const char *char2){
while(*char2){
*char1 = *char2;
char1++;
char2++;
//验证添加char1 = '\0';
if(*char2=='\0'){
printf("退出while循环,不执行*char1 = *char2; 没有赋值给char1结束符");
printf("\n");
}
}
*char1 = '\0';//添加char1 = '\0';
}
int main()
{
char str1[5] = "abcde";
const char* p = "hello";
stringcpy(str1, p);
printf("%s\n", str1);
printf("%s\n", p);
return 0;
}
implement
$ gcc strcpy.c -o strcpy
$ ./strcpy
退出while循环,不执行*char1 = *char2; 没有赋值给char1结束符
hello
hello
Three, strcat function use
1. Use
prototype:
char *strcat ( char * destination, const char * source );
strcat append copy, append to the target space, the target space must be large enough to accommodate the content of the source string
#include<stdio.h>
#include<string.h>
int main()
{
char p1[20] = "Hello";
const char* p2 = "World";
strcat(p1, p2);
printf("%s\n",p1);
return 0;
}
$ gcc strcat.c -o strcat
$ ./strcat
HelloWorld
2. Implementation method
#include<stdio.h>
#include<string.h>
void stringcat(char *str1,const char *str2){
while(*str1){
str1++;
}//str1结束循环,当前是str1的存放值是'\0'
if(*str1=='\0'){
while(*str2){
*str1 = *str2;//str2的首个字符 W,替换赋值当前是str1的存放值是'\0'
str1++;
str2++;
}
str1='\0';
}
}
int main()
{
char p1[20] = "Hello";
const char* p2 = "World";
stringcat(p1, p2);
printf("%s\n",p1);
return 0;
}
implement
$ gcc strcat.c -o strcat
$ ./strcat
HelloWorld
Four, strcmp function use
prototype
int strcmp ( const char * str1, const char * str2 );
strcmp compares the size of two strings, character by character, and compares by ASCII code.
The rule:
the first string is greater than the second string, then return a number greater than 0.
The first string is equal to the second string , returns 0 if
the first string is less than the second string, returns a number less than 0
Note: The strcmp function compares the size of two strings, regardless of the length of the string itself
1. Use
#include<stdio.h>
#include<string.h>
int main()
{
char* p1 = "abc";
char* p2 = "abc";
char* p3 = "abcd";
char* p4 = "e";
printf("----------------------\n");
printf(" %d\n", strcmp(p1,p2 ));
printf("----------------------\n");
printf(" %d\n", strcmp(p2,p3));
printf(" %d\n", strcmp(p3,p2));
printf("----------------------\n");
printf(" %d\n", strcmp(p3,p4));//ASCII 'a' - 'e' = 97 - 101 = -4
printf(" %d\n", strcmp(p4,p3)); //ASCII 'a' - 'e' = 101 - 4 = -4
printf(" %d\n", 'a');
printf(" %d\n", 'e');
}
}
implement
$ gcc strcmp.c -o strcmp
$ ./strcmp
----------------------
0
----------------------
-100
100
----------------------
-4
4
97
101
2. Implementation method
#include<stdio.h>
#include<string.h>
int Strintcmp(const char * str1, const char * str2)
{
//1、两个不同的字符串比较时,直接 return (*str1-*str2);//返回两字符相减的ASCII码值
//2、字符串相同时进入while循环,字符串结束符是'\0',添加一个循环的结束条件!(*str1|*str2)=='\0')
while ((*str1 == *str2)&&(!(*str1|*str2)=='\0')){
str1++;
str2++;
}
return (*str1-*str2);//返回两字符相减的ASCII码值
}
int main()
{
char* p1 = "abc";
char* p2 = "abc";
char* p3 = "abcd";
char* p4 = "e";
printf("----------------------\n");
printf(" %d\n", Strintcmp(p1,p2 ));
printf("----------------------\n");
printf(" %d\n", Strintcmp(p2,p3));
printf(" %d\n", Strintcmp(p3,p2));
printf("----------------------\n");
printf(" %d\n", Strintcmp(p3,p4));
printf(" %d\n", Strintcmp(p4,p3));
printf(" %d\n", 'a');
printf(" %d\n", 'e');
}
implement
$ gcc strcmp.c -o strcmp
$ ./strcmp
----------------------
0
----------------------
-100
100
----------------------
-4
4
97
101