常见字符串问题总结(三)
来源:互联网 发布:淘宝上怎么实名认证 编辑:程序博客网 时间:2024/04/28 09:27
常见字符串问题总结(三)
字符串问题中经常会有删除某一字符串的某一个字符,可以是不同方式的删除,比如将指定的字符删除,删除字符中的空格(包括字符串的头和尾)。本文就列举了部分有关字符串删除的问题。
1. 删除字符串中的空格,要求将字符串前面和后面的所有空格都删除,对字符串中间,如果出现多个连续的空格,只保留一个空格。
#include <iostream>#include <cstring>using namespace std;/*删除一个字符串中的空格,包括前面的和后面的;如果中间字符间的空格大于1个则删除其它的空格,只保留一个空格 */
#define MAXSIZE 100//字符串数组的大小void delspace(char *str){ if(NULL == str) return; char *temp = strdup(str); int i =0; int j =0; while('\x20'== temp[i]) i++; while('\0' != temp[i]) { if(temp[i]!= ' ') str[j++] = temp[i++]; else { while(temp[i] == ' ') i++; str[j++] = ' '; } } if(str[j-1] == ' ') str[j-1] = '\0'; else str[j] = '\0'; //strcpy(str, result); free(temp); }int main(){ char test[MAXSIZE]; gets(test); delspace(test); cout<<strlen(test)<<endl<<test<<endl; return 0;}
2. 查找一个字符串中某个字符出现的频率。
利用hash表就可以实现该问题的解决办法,因为ASSIC码中共有256个字符因此可以建立一个256的hash映射数组,一次遍历字符串,记录每个字符出现的频率。
#include <iostream>using namespace std;int main(){ char str[100]; int hash[256]; memset(hash, 0, sizeof(int)*256); gets(str); char *temp = str; while('\0' != *temp) { hash[*temp + '\0'] +=1; temp++; } int max =0; char ch = '\0'; for(int i =0; i< 256; i++) { if(hash[i] > max) { max = hash[i]; ch = i+'\0'; } } cout<<max<<endl<<ch<<endl; return 0;}
3. 输入若干个字符串,按字典顺序对其排序,并输出。
一种情况是字符串只有大写或者小写字符,这是可以用strcmp进行比较。而strcmp的实现已经在前面的博文中说过了。
#include <iostream>#include <cstring>using namespace std;/*输入五个字符串,按字典顺序对他们进行输出*/int main(){ char str[5][10], temp[10], (*p1)[10], (*p2)[10]; for(p1 = str; p1 < str+5; p1++) gets(*p1); cout<<endl; for(int i =1; i< 5;i++) { for(p1 =str, p2 =str +1;p1<str+5-i;p1++,p2++ ) //冒泡法排序 { if(strcmp(*p1,*p2)>0) { strcpy(temp,*p2); strcpy(*p2, *p1); strcpy(*p1, temp); } } } for(int i =0; i< 5; i++) cout<<str[i]<<endl; return 0;}
另一种情况是字符串中掺杂着大小写字符,这是可以用库函数中的strcasecmp()进行比较,本文中实现了strcasecmp()函数。
#include <iostream>#include <cstring>using namespace std;/*输入五个字符串,按字典顺序对他们进行输出*/char mytolower(char ch){ if(ch >= 'A' && ch <= 'Z') ch = ch - ('A' - 'a'); return ch; }int Mystrcasecmp(char* src, char *dest){ if(NULL == src || NULL == dest) { cout<<"parameter error"; return 0; } while((*src == *dest)&&*src != '\0'&& *dest!='\0') { src++; dest++; } if(mytolower(*src) == mytolower(*dest)) return 0; else if(mytolower(*src) > mytolower(*dest)) return 1; else return -1; }int main(){ char str[5][10], temp[10], (*p1)[10], (*p2)[10]; for(p1 = str; p1 < str+5; p1++) gets(*p1); cout<<endl; for(int i =1; i< 5;i++) { for(p1 =str, p2 =str+1;p1<str+5-i;p1++,p2++ ) { if(Mystrcasecmp(*p1,*p2)>0) { strcpy(temp,*p2); strcpy(*p2, *p1); strcpy(*p1, temp); } } } for(int i =0; i< 5; i++) cout<<str[i]<<endl; return 0;}
每天练一些,继续加油。
- 常见字符串问题总结(三)
- VC常见入门问题总结(三)
- 字符串问题(三)
- 字符串问题(三)
- Linux常见基础命令总结(三)
- Android常见面试题总结(三)
- 常见的字符串面试问题(一)
- 常见的字符串面试问题(二)
- 常见字符串面试问题
- C语言字符串函数总结(部分常见字符串函数)
- python基础(三)字符串的常见操作
- 经典问题总结(三)
- ws问题总结(三)
- 常见javaweb问题总结
- 常见小问题总结
- 常见网络问题总结
- 常见的问题总结
- 常见git问题总结
- C语言小程序(131116)
- Andriod使用浏览器经历
- 从30岁到35岁:为你的生命多积累一些厚度
- 我们今年二十三四岁
- SVN 服务器的搭建及配置
- 常见字符串问题总结(三)
- 面试的技巧
- LeetCode 之 Best Time to Buy and Sell Stock II
- NSArray 折半查找法
- 查看JVM各个代的内存状态
- cocos2d-x-LuaProxy学习日志(5) -- Http网络交互
- #include""和#include<>的区别
- C++中缀转后缀表达式
- DCOM列集散集的内部具体实现的研究初探。