您的位置:首页 > 其它

字符串中删除特定字符

2014-10-11 11:16 267 查看
痛心的感觉!

题目要求:

删除给定字符串中的‘a' 字符,返回原字符串。 遍历一次。

<span style="font-size:14px;">#include <iostream>
#include <string.h>
using namespace std;

char * deleteChar(char * str) {

char * pFast , *pSlow;
int length = strlen(str);
pFast = pSlow = str;
while(length--) {

if(*pFast == 'a') {
++pFast;
} else {
*pSlow = *pFast;
pSlow ++;
pFast ++;
}
}
*pSlow = '\0';

return str;
}
int main() {

char str[]= {"AabcdaaMMaWWacD"};
cout <<str<<endl;
cout<<  deleteChar(str);
return 0;
}
</span>


_____________________________________________________________________________

删除 'a' , 'bc'

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

char * deleteChar(char * str) {

char * pFast , *pSlow;
int length = strlen(str);
pFast = pSlow = str;
while(length--) {
char temp;
if(*pFast == 'a') {
++pFast;
} else if( *pFast == 'b') {
temp = *(pFast +1);
if( temp == 'c') {
pFast +=2;
} else {
++pFast;
}
}

else {
*pSlow = *pFast;
pSlow ++;
pFast ++;
}
}
*pSlow = '\0';
return str;
}
int main() {

char str[]= {"cabbabcdefag"};
cout <<str<<endl;
cout<<  deleteChar(str);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: