基于visual Studio2013解决面试题之0909移动星号

来源:互联网 发布:2017年云计算行业报告 编辑:程序博客网 时间:2024/06/11 13:58



题目



解决代码及点评

/*    函数将字符串中的字符'*' 移到串的前部分但是不能改变非*字符的顺序,比如a**b**c**经过变化后是******abc如果没有顺序的需求,那么只要跟奇偶站队一样进行交换*/#include <iostream>using namespace std;void ChangeStr(char *pszBuf){    int nLen = strlen(pszBuf);    int i = nLen - 1;    int j = nLen - 1;// 从尾巴上开始遍历    while (i >= 0)    {// 如果i不是*号,那么拷贝到j位置,并且j--        if (pszBuf[i] != '*')        {            pszBuf[j--] = pszBuf[i];        }// 如果i位置上是不是*号,那么i都得继续往前移动// 这样的结果是i和j步调不一致        i--;    }// 最后j剩下的位置都填写上*号即可    while (j >= 0)    {        pszBuf[j--] = '*';    }}int main(){    char szBuf[] = "a**b*c**";    ChangeStr(szBuf);    cout<<szBuf<<endl;    system("pause");    return 0;}

代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果








0 0