【C语言】:字符串函数(使用和演示)

一.strlen函数

功能:统计参数 str 指向的字符串的长度。Get string length

参数str:指针,指向了要统计长度的字符串。

返回值:返回了str指向的字符串的长度,由于返回的长度是非负数,故用size_t类型。

#include<stdio.h>
#include<string.h>
int main() {
	const char* str = "abcdef";
	printf("%zd\n", strlen(str));
	return 0;
}

注意: 

 参数的字符串必须要以 '\0' 结尾,因为strlen 是统计 '\0' 之前的字符个数。

函数的返回类型为size_t类型。

使用strlen时要包含头文件<string.h>.


二.strcpy

功能:字符串拷贝。 Copy string

参数destination:指针,指向目的地空间。source:指针,指向源头数据。

返回值:strcpy函数返回的目标空间的起始地址(destination)

#define   _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdio.h>
int main() {
	char arr1[10] = { 0 };
	char arr2[] = "hello";
	strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

输出:hello;

注意

源字符串(source)必须要以‘ \0’ 结束,并且strcpy函数会将源字符串(source)拷贝到目标空间(destination)。

目标空间(destination)足够大,确保能存放源字符串(source)并且目标空间(destination)可以被修改(前提是不被const修饰)。To avoid overflows


三.strcat

功能:字符串追加(连接字符串)。Concatenate strings

参数destination:指针,指向目的地空间。source:指针,指向源头数据。

返回值:strcpy函数返回的目标空间的起始地址(destination)

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

int main() {
	char arr1[20] = "hello ";
	char arr2[] = "worle";
	strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

注意

源字符串要以'\0'结束。

目标字符串也要有 '\0' ,否则没办法知道从哪里开始追加。

目标空间(destination)足够大,确保能存放源字符串(source)并且目标空间(destination)可以被修改(前提是不被const修饰)。To avoid overflows 


四.strcmp

功能:比较两个字符串,str1,str2指向的字符串,如果它们的ASCLL码值彼此相等,则继续处理以下对,直到字符不同或到达终止 null 字符。Compare two strings

参数str1:指针,指向要比较的字符串。str2:指针,指向要比较的另一个字符串。

返回值规定

第一个字符串大于第二个字符串,则返回一个大于零的数

the first character that does not match has a lower value in ptr1 than in ptr2

第一个字符串等于第二个字符串,则返回一个等于零的数

the contents of both strings are equal

第一个字符串小于第二个字符串,则返回一个小于零的数

the first character that does not match has a greater value in ptr1 than in ptr2

#include<stdio.h>
#include<string.h>
int main() {
	char arr1[] = "abcdef";
	char arr2[] = "abq";
	int r = strcmp(arr1, arr2);
	printf("%D\n", r);
	return 0;
}

五.strncpy,strncat,strncmp的简单介绍

都是在函数strcpy,strcat,strcmp的基础上多了一个参数size_t num作用是指定函数作用在目标空间的字符个数,效果是相对来说更加安全,更灵活。


 六.strstr

功能:查找str2指向的字符串在str1指向的字符串(被查找)中第一次出现的位置。

Locate substring

头文件:<string,h>

参数:str1指针,指向被查找的字符串,str2指针,指向要查找的字符串。

返回值:

如果str1指向的字符串中存在str2指向的字符串,那木返回第一次出现位置的指针

如果str1指向的字符串中不存在str2指向的字符串,那么返回NULL

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

#include<stdio.h>
#include<string.h>
int main() {
	char str[] = "This is simple string";
	char* pch;
	pch = strstr(str, "simple");
	if (pch != NULL)
		printf("%s\n", pch);
	else
		printf("查找的字符串不存在\n");
	return 0;
}


七.strtok

功能:分割字符串:根据delim参数指定的分隔符,将输入的字符串str拆分成多个子字符串;

修改原始字符串:strtok 会在原始字符串中插入‘\0’终止符,替换分隔符的位置,因原始字符串会被修改

Split string into tokens 

参数str:首次调用时传入分隔的字符串;后续调用传入NULL,表示继续分割同一个字符串

C string to truncate 

delim:包含所有可能分隔符的字符串

C string containing the delimiter characters.

返回值:成功时返回指向当前字符串的指针;没有更多子字符串时返回NULL。

If a token is found, a pointer to the beginning of the token.Otherwise, a null pointer.null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.

#define   _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
	char arr[] = "192.168.0.101";
	const char* sep = ".";
	const char* str = NULL;
	char buf[30] = { 0 };
	strcpy(buf, arr);//将arr中的字符串拷贝到buf中,对buf的内容进行切割
	for (str = strtok(buf, sep);str != NULL;str = strtok(NULL, sep)) {
		printf("%s\n", str);
	}
	return 0;
}

 

八.strerror

功能:获取指向错误消息字符串的指针。Get pointer to error message string

参数errnum:表示错误码。 Error number(需要包含头文件<srrno.h>

返回值:函数返回通过错误码得到的错误信息字符串的抽字符的地址。

A pointer to the error string describing error errnum

#define   _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdio.h>
int main() {
	int i = 0;
	for (i = 0;i <= 10;i++) {
		printf("%d:%s\n",i, strerror(i));
	}
	return 0;
}

输出

0:No error
1:Operation not permitted
2:No such file or directory
3:No such process
4:Interrupted function call
5:Input/output error
6:No such device or address
7:Arg list too long
8:Exec format error
9:Bad file descriptor
10:No child processes


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值