痛心的感觉!
题目要求:
删除给定字符串中的‘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;
}