C - C++ - strcmp - strncmp

本文介绍了由Yongqiang Cheng编写的C++自定义字符串比较函数`yong_strcmp`、`qiang_strcmp`和`cheng_strcmp`,以及`yong_strncmp`函数。通过示例展示了这些函数如何比较字符串,并与标准库中的`strcmp`和`strncmp`函数进行对比。文章还包含了多个测试用例,展示了不同字符串比较的结果。
摘要由CSDN通过智能技术生成

C - C++ - strcmp - strncmp

4. yongqiangcheng - strcmp

int yong_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	for (i = 0; str1[i] != '\0'; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return str1[i] - str2[i];
}

bool qiang_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return false;
	}

	while (*str1 && (*str1 == *str2))
	{
		++str1;
		++str2;
	}

	return *str1 == *str2;
}

int cheng_strcmp(const char str1[], const char str2[])
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	while (str1[i] != '\0' && (str1[i] == str2[i]))
	{
		++i;
	}

	return str1[i] - str2[i];
}

4.1 Example 1

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	const char str1[] = "yong qiang";
	const char str2[] = "yong qiang";
	int ret = 0;

	ret = strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	return(0);
}
str1 is equal to str2
请按任意键继续. . .

4.2 Example 2

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	const char str1[] = "1 yong qiang";
	const char str2[] = "2 yong qiang";
	int ret = 0;

	ret = strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	return(0);
}
str1 is less than str2
请按任意键继续. . .

4.3 Example 3

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	const char str1[] = "y yong qiang";
	const char str2[] = "q yong qiang";
	int ret = 0;

	ret = strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	return(0);
}
str2 is less than str1
请按任意键继续. . .

4.4 Example 4

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int yong_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	for (i = 0; str1[i] != '\0'; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return str1[i] - str2[i];
}

bool qiang_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return false;
	}

	while (*str1 && (*str1 == *str2))
	{
		++str1;
		++str2;
	}

	return *str1 == *str2;
}

int main()
{
	const char str1[] = "yong qiang";
	const char str2[] = "yong qiang";
	int ret = 0;

	ret = yong_strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	bool res = false;
	res = qiang_strcmp(str1, str2);

	if (res) {
		printf("true - equal\n");
	}
	else {
		printf("false - unequal\n");
	}

	return(0);
}
str1 is equal to str2
true - equal
请按任意键继续. . .

4.5 Example 5

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int yong_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	for (i = 0; str1[i] != '\0'; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return str1[i] - str2[i];
}

bool qiang_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return false;
	}

	while (*str1 && (*str1 == *str2))
	{
		++str1;
		++str2;
	}

	return *str1 == *str2;
}

int main()
{
	const char str1[] = "1 yong qiang";
	const char str2[] = "2 yong qiang";
	int ret = 0;

	ret = yong_strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	bool res = false;
	res = qiang_strcmp(str1, str2);

	if (res) {
		printf("true - equal\n");
	}
	else {
		printf("false - unequal\n");
	}

	return(0);
}
str1 is less than str2
false - unequal
请按任意键继续. . .

4.6 Example 6

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int yong_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	for (i = 0; str1[i] != '\0'; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return str1[i] - str2[i];
}

bool qiang_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return false;
	}

	while (*str1 && (*str1 == *str2))
	{
		++str1;
		++str2;
	}

	return *str1 == *str2;
}

int main()
{
	const char str1[] = "y yong qiang";
	const char str2[] = "q yong qiang";
	int ret = 0;

	ret = yong_strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	bool res = false;
	res = qiang_strcmp(str1, str2);

	if (res) {
		printf("true - equal\n");
	}
	else {
		printf("false - unequal\n");
	}

	return(0);
}
str2 is less than str1
false - unequal
请按任意键继续. . .

4.7 Example 7

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int yong_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	for (i = 0; str1[i] != '\0'; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return str1[i] - str2[i];
}

bool qiang_strcmp(const char *str1, const char *str2)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return false;
	}

	while (*str1 && (*str1 == *str2))
	{
		++str1;
		++str2;
	}

	return *str1 == *str2;
}

int cheng_strcmp(const char str1[], const char str2[])
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	int i = 0;
	while (str1[i] != '\0' && (str1[i] == str2[i]))
	{
		++i;
	}

	return str1[i] - str2[i];
}

int main()
{
	const char str1[] = "y yong qiang";
	const char str2[] = "q yong qiang";
	int ret = 0;

	ret = cheng_strcmp(str1, str2);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	bool res = false;
	res = qiang_strcmp(str1, str2);

	if (res) {
		printf("true - equal\n");
	}
	else {
		printf("false - unequal\n");
	}

	return(0);
}

str2 is less than str1
false - unequal
请按任意键继续. . .

5. yongqiangcheng - strncmp

int yong_strncmp(const char *str1, const char *str2, const int len)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	for (int i = 0; i < len; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return 0;
}

5.1 Example 1

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int yong_strncmp(const char *str1, const char *str2, const int len)
{
	if (NULL == str1 || NULL == str2)
	{
		// TODO - yongqiang cheng
		return 0;
	}

	for (int i = 0; i < len; ++i)
	{
		if (str1[i] != str2[i])
		{
			return str1[i] - str2[i];
		}
	}

	return 0;
}

int main()
{
	const char str1[] = "yong1";
	const char str2[] = "yong2";
	int ret = 0;

	ret = yong_strncmp(str1, str2, 4);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	ret = yong_strncmp(str1, str2, 5);

	if (ret < 0) {
		printf("str1 is less than str2\n");
	}
	else if (ret > 0) {
		printf("str2 is less than str1\n");
	}
	else {
		printf("str1 is equal to str2\n");
	}

	return(0);
}
str1 is equal to str2
str1 is less than str2
请按任意键继续. . .

References

https://www.cplusplus.com/reference/cstring/strcmp/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

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

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

打赏作者

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

抵扣说明:

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

余额充值