字符串的操作对于日常工作、面试中还是很重要的,可以说随时都在使用,所以掌握一些基本的字符串操作算法还是有这个必要的。
就我个人而言,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");
}
就这么多啦。。。以后用到了,不要太感谢我哟~~~~~