贴出代码。来在于微软笔试:
#include<string.h>
#include <iostream>using namespace std;
char *f(char *str,char ch)
{
char *it1=str;
char *it2=str;
while(*it2!='\0')
{
while(*it2==ch)
{
it2++;
}
// cout<<"one "<<*it1;
*it1++=*it2++;
// cout<<*it1<<" ";
// cout<<"2 "<<*it2;
}
return str;
}
void main(int argc,char *argv[])
{
char *a=new char[10];
strcpy(a,"abcdcccd");
cout<<f(a,'c');
system("pause");
}
运行后结果:
abddcccd
分析:
*it1++=*it2++等价于:
*it1=*it2;
it1++;it2++;