删除字符串中任意指定的字符
2013-04-16 23:20
316 查看
声明:以下题目来自网络
思想:八位的char型字符而言,建一个大小为256的数组,把所有元素都初始化为0。然后对于字符串中每一个字符,把它的ASCII码映射成索引,把数组中该索引对应的元素设为1,这样判断是否是要删除的字符时间复杂度就成O(1)了
删除字符时,设置两个指针*pFast,*pSlow,如果*pFast指向的不是要删除的字符把*pFast指向的值赋值给*pSlow,然后两个指针都向后迁移,若*pFast指向的是删除的字符*pFast++判断下一个字符情况
思想:八位的char型字符而言,建一个大小为256的数组,把所有元素都初始化为0。然后对于字符串中每一个字符,把它的ASCII码映射成索引,把数组中该索引对应的元素设为1,这样判断是否是要删除的字符时间复杂度就成O(1)了
删除字符时,设置两个指针*pFast,*pSlow,如果*pFast指向的不是要删除的字符把*pFast指向的值赋值给*pSlow,然后两个指针都向后迁移,若*pFast指向的是删除的字符*pFast++判断下一个字符情况
#include <stdio.h> #include <conio.h> char TheArray[256]; void InitTheArray(const char * szFind) { while('\0'!=*szFind) TheArray[*szFind++] = 1; } void ProcessTheString(char * szDestination) { char * pFast; char * pSlow; pFast = pSlow = szDestination; while ('\0'!=*pFast) { if(0==TheArray[*pFast]) *pSlow++ = *pFast++; else pFast++; } *pSlow = '\0'; } int main() { char szDes[] = "They are Students."; char szFind[] = "aeiou"; InitTheArray(szFind); ProcessTheString(szDes); printf("%s", szDes); getch(); return 0; }
相关文章推荐
- 【我解C语言面试题系列】011 删除指定字符串的相应字符
- 1.删除字符串中重复的字符 2.删除字符串中指定的字符串 3.删除数组中重复元素
- 从0开始<五>:字符串相关-htoi函数,删除字符串中指定字符
- python 删除字符串中指定字符&去掉空格
- Java中如何从一个字符串中删除指定字符
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 字符串的一些基本操作,复制,长度,子串查找,单词反转,比较,字符串反转,删除指定字符。
- 在一个给定的字符串中删除指定的字符
- javascript 在字符串中任意操作指定位置字符的方法
- Java中如何从一个字符串中删除指定字符
- 【我解C语言面试题系列】011 删除指定字符串的相应字符
- oracle 删除字符串头尾的指定一个字符
- 删除字符串中指定的字符形成字符串
- 字符串中删除指定的字符
- 删除字符串中指定的字符
- [转载]c删除字符串中指定字符
- 程序员面试指南(第六章) 数组和字符串(C#版) 2.从字符串中删除指定的字符
- 从字符串指定位置删除指定个数的字符
- C学习笔记--在字符串中删除指定的字符
- Java随机生成长度不少于6的指定长度的密码,且这个字符串必须包含大小写字母、数字和特殊字符,四种的任意三种