若干关于 字符串 的小算法

本文详细介绍了C++中几种常见的字符串操作方法,包括字符串拷贝、子串计数、去空白、反转及键值对获取等。通过具体代码实例,深入探讨了每种操作的实现原理和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串的操作对于日常工作、面试中还是很重要的,可以说随时都在使用,所以掌握一些基本的字符串操作算法还是有这个必要的。        

         就我个人而言,C++处理字符串还是不行呀,尽管提供了不少库。众所周知,处理字符串最牛逼的语言是Python,但是这里不讨论,以下为列出来了几种关于字符串的常用操作小案例,以防面试考,你懂的。


一、拷贝字符串的实现

  第一种方法:

int  copy_str(char *from , char *to)
{
	int ret = 0;
	//作为资深程序员,应当具备的素质
	if (from ==NULL || to== NULL)
	{
		ret = -1;
		printf("func copy_str2() err: %d, (from ==NULL || to== NULL)", ret);
		return ret;
	}
	
	for (; *from!='\0'; from ++, to++ )
	{
		*to = *from;
	}
	*to = '\0';
	return ret;
}


第二种:

void copy_str(char *from , char *to)
{
	while(*from != '\0')
	{
		*to ++ = *from++;
	}
	*to = '\0';
}
第三种: 也是最好的一种,最装逼的一种。

void copy_str4(char *from , char *to)
{
	while((*to ++ = *from++) != '\0')
	{
		;
	}
}

测试代码:

void main()
{
	int rv = 0;
	char from[100] = {0};
	char to[100] = {0};
	strcpy(from, "fromabc");
	rv = copy_str(from, to);
	if (rv != 0)
	{
		printf("func copy_str2:%d", rv);
		return ;
	}
	printf("%s", to);
	system("pause");
}



二、计算一个字符串中包含子串的数目

int main()
{
	char * str = "abcd12kjkdljfabcddkjslabcddkjflkasdjfabcd";
	char * res = "abcd";
	int count = 0;
	char * p;
	while (*str != '\0')
	{
		p = strstr( str, res);
		str = p;
		if (p != NULL)
		{
			count ++;
			str = str + strlen(res);
		}
		else
		{
			break;
		}
	}

	cout << "count = " << count << endl;

	system("pause");
	return 0;
}


三、对字符串进行两边去空白操作

#include "stdafx.h"
#include <iostream>
using namespace std;

int trim_str(char * in, char * out)
{
	int i = 0, j = strlen(in) -1 ;
	int str_count = 0;
	while (isspace(in[i])  && in[i] != '\0')
	{
		i++;
	}
	while (isspace(in[j] && j > 0))
	{
		j--;
	}
	str_count = j-i + 1;  //拷贝的起始位置

	memcpy( out, in+i, str_count);
	out[str_count] = '\0';

	return 0;
}

void main()
{
	char * str = "   abcde    ";
	char buf[100];
	int ret = 0;
	cout << str << endl;
	ret = trim_str( str, buf);
	if (0 != ret)
	{
		printf("func trimSpace() err:%d\n", ret);
		return ;
	}
	cout << "str:"<< str << endl;
	cout << "buf: " << buf << endl;
	system("pause");
}


四、字符串反转

void main()
{
	//char * str = "abcde";
	char str[] = "abcde";
	char *p1 = str;
	char *p2 = str + strlen(str) - 1;
	char  temp ;
	cout << str << endl;
	while (p1 < p2)
	{
		temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++; p2--;
	}
	cout << str << endl;
	system("pause");
}


五、模拟键值对,通过key获得value

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int trim_space( char * in_valuebuf, char * out_valuebuf)
{
	int count = 0;
	int i = 0, j = strlen(in_valuebuf) - 1;
	while (isspace(in_valuebuf[i]) && in_valuebuf[i] != '\0')
	{
		i++;
	}
	while (isspace(in_valuebuf[j]) && j > 0)
	{
		j--;
	}
	memcpy( out_valuebuf, in_valuebuf+i, j-i+1);
	out_valuebuf[j-i+1] = '\0';
	return 0;
}

int get_value_by_key( char * keyvalue, char * key, char * value, int * value_len)
{
	char recv_value_buf[1024*10];
	char * p = strstr( keyvalue, key);
	if (NULL == p)
	{
		return 0;
	}
	p = p + strlen(key);

	p = strstr( keyvalue, "=");
	if (NULL == p)
	{
		return 0;
	}
	p = p + 1;
	if (trim_space( p, recv_value_buf) != 0)
	{
		cout << "function trim_space err : " << trim_space( p, recv_value_buf) << endl;
		return -1;
	}
	strcpy_s( value, strlen(recv_value_buf)+1, recv_value_buf);
	*(value_len) = strlen(recv_value_buf);
	return 0;
}

void main()
{
	char * str = " name =   zhangyuqing";
	char * key = "name";
	int  len = 0;
	char value[1024*10];
	int n = get_value_by_key( str, key, value, &len);
	if (0 != n)
	{
		cout << "function get_value_by_key err : " << n << endl;
		return ;
	}
	cout << "value = " << value << endl;
	cout << "len =   " << len << endl;

	cout << strlen(value) << endl;
	cout << sizeof(value) << endl;

	system("pause");
}



就这么多啦。。。以后用到了,不要太感谢我哟~~~~~



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值