若干关于 字符串 的小算法
2014-08-19 15:07
169 查看
字符串的操作对于日常工作、面试中还是很重要的,可以说随时都在使用,所以掌握一些基本的字符串操作算法还是有这个必要的。
就我个人而言,C++处理字符串还是不行呀,尽管提供了不少库。众所周知,处理字符串最牛逼的语言是Python,但是这里不讨论,以下为列出来了几种关于字符串的常用操作小案例,以防面试考,你懂的。
一、拷贝字符串的实现
第一种方法:
第二种:
测试代码:
二、计算一个字符串中包含子串的数目
三、对字符串进行两边去空白操作
四、字符串反转
五、模拟键值对,通过key获得value
就这么多啦。。。以后用到了,不要太感谢我哟~~~~~
就我个人而言,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"); }
就这么多啦。。。以后用到了,不要太感谢我哟~~~~~
相关文章推荐
- 设计一个程序,从键盘上输入若干字符串,利用算法库中的查找函数对给定的字符串进行查找,将查找后的结果输出
- 关于字符串“循环左移”算法的讨论
- 关于输入一个字符串,对其进行全排列的两种算法
- 关于若干选举算法的解释与实现
- [算法] 关于algs4 MSD.java 高位优先的字符串排序 的逐行代码解释
- 发现Python的源代码中关于字符串fastsearch算法的一个笔误!
- 关于数组字符串中重复的值问题类似算法题
- 关于字符串的常用算法
- 关于字符串的若干小结
- 关于字符串的几个算法
- 关于对称字符串的算法
- 关于字符串算法的总结
- 数组元素几个关于串的小算法题:最小K个数、连续子数组的最大和、字符串全排列求法、数组循环移位
- 关于有序二维矩阵查找和字符串替换的两道算法题
- 关于字符串处理的一系列类似算法
- 关于字符串的一些算法
- 几个关于串的小算法题:最小K个数、连续子数组的最大和、字符串全排列求法、数组循环移位
- 关于字符串编码的(ANSI、GB2312、UTF8)问题,在应用开发中若干问题
- 关于manacher算法求字符串中最长字符串和其长度
- [noip][c/c++]关于字符串中前导0和后导0的处理算法