C语言--字符串和字符串函数(二)

字符串函数

C库提供了很多处理字符串的函数,这些函数原型在string.h头文件中。

strlen()函数

strlen()函数用于统计字符串的长度。
示例:

#include<stdio.h>
#include<string.h>
int main(void)
{
	char str[] = "This is a string.";

	printf("str has %d characters.\n", strlen(str));

	return 0;
}

运行结果:
在这里插入图片描述
可以看出这个函数计算出来的长度是不带‘\0’的。

可以编写一个修改字符串长度的函数。

void fit(char* str, int size)
{
	if (strlen(str) > size)
		str[size] = '\0';
}

strcat()函数

用于拼接字符串, 函数接收两个字符串作为参数,把第二个字符串的备份附加在第一个字符串的末尾,并把拼接后形成的新字符串作为第一个字符串,第二个字符串不变。

用法:

#include<stdio.h>
#include<string.h>
#include<assert.h>
#define SIZE 128
char* s_gets(char* st, int n);

int main(void)
{
	char str1[SIZE] = "Hello, my name is Tarzan.";
	char str2[SIZE];
	puts("What's your job?");
	if (s_gets(str2, SIZE))
	{
		strcat_s(str1, str2);
		puts("After strcat:");
		puts(str1);
	}
	else
	{
		puts("End of file encountered.");
	}

	return 0;
}

char* s_gets(char* st, int n)
{
	assert(st != nullptr);
	char* ret_val = fgets(st, n, stdin);
	int i = 0;

	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			++i;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

运行结果:
在这里插入图片描述

strncat()函数

strcat()函数无法检查第一个数组能否容纳下第二个字符串,strncat()函数,该函数的第三个参数指定了最大添加字符数。
例如:

strnact(str1, str2, 10);

strcmp()函数

该函数比较字符串内容的大小,而不是字符串地址。
程序示例:

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

int main(void)
{
	 char str[4][10] = {
		"China",
		"Russia",
		"America",
		"Japan"
	};
	 int i, j;
	 for (i = 1, j = 0; i < 4; ++i)
	 {
		 if (strcmp(str[i], str[j]) > 0)
			 j = i;
	 }
	 printf("%s\n", str[j]);
	 return 0;
}

此程序打印几个字符串比较之后最大的字符串
运行结果:
在这里插入图片描述

strcmp()的返回值

直接拿程序看:

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

int main(void)
{
	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 0;
}

运行结果:
在这里插入图片描述
这说明:如果字母表中第一个字符串位于第二个字符串前面,strcmp()函数返回负数,反之返回正数。

如果两个字符串开始几个字符都相同怎么办?
一般情况,strcmp()会一次比较每个字符,直到发现第一对不同的字符为止。

strncmp()函数

该函数在比较俩个字符串的时候,可以比较到字符不同的地方,也可以只比较第三个参数指定的字符数。
示例:

#include<stdio.h>
#include<string.h>
#define SIZE 6
int main(void)
{
	const char* str[SIZE] =
	{
		"astronomy", "astounding",
		"astrophysics", "ostracize",
		"asterism", "astrophobia"
	};
	int i = 0;
	for (i = 0; i < SIZE; ++i)
	{
		if (strncmp(str[i], "astro", 5) == 0)
		{
			printf("Found: %s\n", str[i]);
		}
	}
	
	return 0;
}

运行结果:
在这里插入图片描述

strcpy()和strncpy()函数

如果希望拷贝整个字符串,则使用strcpy()函数,它相当于字符串赋值运算符。

示例:

#include<stdio.h>
#include<string.h>
#define SIZE 40
int main(void)
{
	char str1[SIZE];
	char str2[SIZE] = { "I wanna be a programmer." };
	strcpy_s(str1, str2);
	puts(str1);
	return 0;
}

运行结果:
在这里插入图片描述

strcpy()的其他属性

  • strcpy()的返回类型是char* , 该函数返回的是第一个参数的值,即第一个字符的地址。
  • 第一个参数不必指向数组的开始。

示例:

#include<stdio.h>
#include<string.h>
#define SIZE 50
int main(void)
{
	char str1[SIZE] = "I wanna be ";
	char str2[SIZE] = "a C programmer.";
	char* ptr;

	ptr = strcpy_s(str1 + 4, str2);
	puts(ptr);
	return 0;
}

但我的编译器VS2019好像不支持这样做。。

strncpy()函数

拷贝字符串用此函数更安全,该函数第三个参数指明可拷贝的最大字符数。

格式:

strncpy(target, source, n);

示例:

#include<stdio.h>
#include<string.h>
#define SIZE 40
int main(void)
{
	char str1[SIZE];
	char str2[SIZE] = "He is a handsome man.";
	strncpy_s(str1, str2, 9);

	puts(str1);
	return 0;
}

运行结果:
在这里插入图片描述

sprintf()函数

该函数声明在stdio.h中,和printf()函数不同,它是把数据写入字符串,而不是打印在显示器上。
所以该函数可以把多个元素组合成一个字符串。
sprintf()函数的第一个参数是目标字符串的地址,其余参数和printf()相同。

示例:

#include<stdio.h>
#include<assert.h>
#include<string.h>
#define LEN 20
#define SIZE 50
char* s_gets(char* st, int n);

int main(void)
{
	char first[LEN];
	char last[LEN];
	char formal[SIZE];
	double prize;

	puts("Please input your first name:");
	s_gets(first, LEN);
	puts("Please input your last name:");
	s_gets(last, LEN);
	puts("Enter your prize money");

	scanf_s("%lf", &prize);
	sprintf_s(formal, "%s, %-12s: $%6.2f\n", last, first, prize);
	puts(formal);
	return 0;
}

char* s_gets(char* st, int n)
{
	assert(st != nullptr);
	char* ret_val = fgets(st, n, stdin);
	int i = 0;
	if (ret_val)
	{
		while (st[i] != '\0' && st[i] != '\n')
			++i;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

运行结果:
在这里插入图片描述

其他字符串函数

一些常用的字符串:

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

如果s字符串中包含c字符, 该函数返回指向s字符串首次出现的c字符的指针。;如果未找到,则返回空指针。

char* strrchr(const char*s, char c);

该函数返回s字符串中c字符的最后一次出现的位置(末尾的空字符也是字符串的一部分,在查找范围内),未找到则返回空指针。

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

如果字符串str1中包含s2字符串中的任意字符,该函数返回指向s1字符串首位置的指针;不包含则返回空指针。

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

该函数返回指向str1字符串中str2字符串出现的首位置。如果在s1中未找到,则返回空指针。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_索伦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值