/*
2005 年11 月金山笔试题。
函数将字符串中的字符'*'移到串的前部分,前面的非'*'字符后移,但不能改变非'*'字符的先后顺序,
函数返回串中字符'*'的数量。如原始串为:abcde*12,处理后为*abcde12,函数并返回值为1。(要求使用尽量少的时间和辅助空间)。
*/
#include<iostream>
using namespace std;
int strMove(char* str)
{
int i = strlen(str)-1; //i遍历字符串中的*
int j = i; //j遍历字符串中的非*字符
int count = 0; //记录'*'的个数
while(j > 0&&str[j] !='*') //跳过最后面的非'*'字符
j--;
while(j >= 0 &&i >=0)
{
while(str[i]!='*' &&i >=0)
i--;
while(str[j] == '*' &&j>=0)
{
j--;
count++;
}
//交换j.i处字符
str[i--] = str[j]; //*前面的字符后移放到*的位置
str[j--] = '*'; //将*前移
}
return count;
}
int main()
{
char str[] = "ab*cdefg*123*45";
cout<<str<<endl;
cout<<"num of * is:"<<strMove(str)<<endl;
cout<<str<<endl;
return 0;
}
#endif