痛心的感觉!
题目要求:
删除给定字符串中的‘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;
}
本文通过两个实例展示了如何使用C++进行字符串操作,包括删除指定字符及特定子串的方法。通过对源代码的详细解析,帮助读者理解算法实现,并提供了一个实际运行的例子。

被折叠的 条评论
为什么被折叠?



