修改字符串

来源:互联网 发布:windows pe img镜像 编辑:程序博客网 时间:2024/05/21 10:00
#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;}

0 0
原创粉丝点击