码迷,mamicode.com
首页 > 其他好文 > 详细

修改字符串

时间:2015-01-06 17:57:44      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:华为入职练习 华为oj

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "oj.h"

int ChangeStringOnce(char *pInStr, char *pOutStr)
{
    if (NULL == pInStr || NULL == pOutStr)
    {
        return -1;
    }

    int iInStrLen =0;
    int iCur =0;
    while ('\0' != pInStr[iCur])//统计字符串长度
    {
        iInStrLen++;
        iCur++;
    }

    if (iInStrLen <=2)
    {
        iCur= 0;
        while ('\0' != pInStr[iCur])
        {
            pOutStr[iCur] = pInStr[iCur];
            iCur++;
        }
        pOutStr[iCur] = '\0';
        return 0; //表示没有删除任何字符
    }
    int flag = 0;
    iCur =0;
    int iOutCur = 0;
    while ('\0' != pInStr[iCur] && '\0' != pInStr[iCur + 1] && '\0' != pInStr[iCur + 2])
    {
        if (pInStr[iCur] <= 'z' && pInStr[iCur]>= 'a' && pInStr[iCur+1] != '\0' && pInStr[iCur] == pInStr[iCur + 1]  && pInStr[iCur] == pInStr[iCur + 2])
        {
            pOutStr[iOutCur] = 'a' + (pInStr[iCur] - 'a' +1)%26;
            iOutCur++;
            iCur += 3;
            flag = 1;
        }
        else
        {
            pOutStr[iOutCur] = pInStr[iCur];
            iOutCur++;
            iCur++;
        }
    }
    while('\0' != pInStr[iCur] )//将可能剩余的字符处理完
    {
        pOutStr[iOutCur] = pInStr[iCur];
        iOutCur++;
        iCur++;
    }
    pOutStr[iOutCur] = '\0';
    return flag ;

}

int ChangeString(char *pInStr,char *pOutStr)
{
    if (NULL == pInStr || NULL == pOutStr)
    {
        return -1;
    }

    int iCur = 0;
    int iInStrLen =0;
    while ('\0' != pInStr[iCur])
    {
        iInStrLen++;
        iCur++;
    }
    char *cTempInput = new char[iInStrLen+1];
    memcpy(cTempInput, pInStr,sizeof(char) * (iInStrLen+1) );

    char *temp = new char[iInStrLen+1];
    memset(temp, 0, sizeof(char) *(iInStrLen+1));

    int flag = 1;
    while (1 == flag)
    {
        flag = ChangeStringOnce(cTempInput, temp);
        memset(cTempInput, 0 , sizeof(char)*iInStrLen);
        memcpy(cTempInput,temp, iInStrLen);
    }
    iCur = 0;
    while ('\0' != temp[iCur])
    {
        pOutStr[iCur] = temp[iCur];
        iCur++;
    }
    pOutStr[iCur] = '\0';
    delete [] cTempInput;
    cTempInput = NULL;
    delete [] temp;
    temp = NULL ;
    return 0;
}


int main()
{
    char *pIn = "acccd";
    char *pOut = "ae";
    char pTest[200];

    ChangeString(pIn, pTest);
    printf("%s",pTest);
    return 0;
}

修改字符串

标签:华为入职练习 华为oj

原文地址:http://blog.csdn.net/xiaohanstu/article/details/42461531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!