首页 > 代码库 > 修改字符串
修改字符串
#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; }
修改字符串
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。