C语言编程实现,将给定字符串中连续出现3次的小写字母替换为改小写字母在字母表中的下一个字母

将给定字符串中连续出现3次的小写字母替换为改小写字母在字母表中的下一个字母,大写字母和其他字符不处理,仍然保留。要求最终输出的字符串中不再存在任何连续出现3次的小写字母。

int ChangeString(char *pInStr,char *pOutStr)
int 0:处理成功 -1:出现异常
例如
pInStr= "lIdddjilfff";
pOutStr= "lIejilg";
pInStr= "aaaabazzzAs";
pOutStr= "babaaAs";
pInStr= "aaaaaaaaacc";
pOutStr= "d";

代码如下:

#include<stdio.h>
#include<string.h>

int ChangeString(char *pInStr,char *pOutStr)
{
strcpy(pOutStr, pInStr);
bool bChange = false;
int iCnt = -1;
do 
{
bChange = false;

iCnt = 0;
while (*(pOutStr + iCnt) != '\0')
{
++iCnt;
}
iCnt--;

for (int i = 0; i < iCnt; ++i)
{
char *pCur = pOutStr + i;
if (*pCur != '\0'
&& *(pCur + 1) != '\0'
&& *(pCur + 2) != '\0'
&& *pCur == *(pCur + 1)
&& *(pCur + 1) == *(pCur + 2)
&& *pCur <= 'z'
&& *pCur >= 'a')
{
if (*pCur != 'z')
{
++*pCur;
}
else
{
*pCur = 'a';
}

int j;
for (j = 3; *(pCur + j) != '\0'; ++j)
{
*(pCur + j - 2) = *(pCur + j);
}
*(pCur + j - 2) = '\0';

bChange = true;
break;
}
}

} while(bChange);

return 0;
}

void main()
{
char* pInStr= "aaaaaaaaacc";
char pOutStr[1024]= "";
if (0 == ChangeString(pInStr, pOutStr))
{
printf("In : %s\n", pInStr);
printf("Out : %s\n", pOutStr);
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-01-12
#include <stdio.h>
int ChangeString(char *pInStr,char *pOutStr)
{
    int pos=0,change=0;
    bool t;
    char a[100],b[100],*instr=a,*outstr=b;
    while(true)
    {
        *(instr+pos)=*(pInStr+pos);
        if(*(pInStr+pos)=='\0')
            break;
        pos++;
    }
    while(true)
    {
        change=0;
        pos=0;
        for(int n=0;*(instr+n)!='\0';n++)
        {
            t=false;
            if(*(instr+n)>='a' && *(instr+n)<='z')
            {
                if(*(instr+n+1)!='\0' && *(instr+n+2)!='\0')
                {
                    if(*(instr+n)==*(instr+n+1) && *(instr+n)==*(instr+n+2))
                    {
                        if(*(instr+n)=='z')
                            *(outstr+pos)='a';
                        else
                            *(outstr+pos)=*(instr+n)+1;
                        pos++;
                        n+=2;
                        t=true;
                        change=1;
                    }
                }
            }
            if(t==false)
            {
                *(outstr+pos)=*(instr+n);
                pos++;
            }
        }
        *(outstr+pos)='\0';
        pos=0;
        while(true)
        {
            *(instr+pos)=*(outstr+pos);
            if(*(outstr+pos)=='\0')
                break;
            pos++;
        }
        if(change==0)
            break;
    }
    pos=0;
    while(true)
    {
        *(pOutStr+pos)=*(instr+pos);
        if(*(instr+pos)=='\0')
            break;
        pos++;
    }
    return 0;
}
int main()
{
    char a[100],b[100];
    scanf("%s",a);
    ChangeString(a,b);
    printf("%s\n",b);
    return 0;
}

第2个回答  2017-10-18
#include <stdio.h>
#include <string.h>

int ChangeString(char *pInStr,char *pOutStr)
{
    strcpy(pOutStr, pInStr);
    bool bChange = false;
    int iCnt = -1;
    do
    {
        bChange = false;

        iCnt = 0;
        while (*(pOutStr + iCnt) != '\0')
        {
            ++iCnt;
        }
        iCnt--;

        for (int i = 0; i < iCnt; ++i)
        {
            char *pCur = pOutStr + i;
            if (*pCur != '\0'
                && *(pCur + 1) != '\0'
                && *(pCur + 2) != '\0'
                && *pCur == *(pCur + 1)
                && *(pCur + 1) == *(pCur + 2)
                && *pCur <= 'z'
                && *pCur >= 'a')
            {
                if (*pCur != 'z')
                {
                    ++*pCur;
                }
                else
                {
                    *pCur = 'a';
                }

                int j;
                for (j = 3; *(pCur + j) != '\0'; ++j)
                {
                    *(pCur + j - 2) = *(pCur + j);
                }
                *(pCur + j - 2) = '\0';

                bChange = true;
                break;
            }
        }

    } while(bChange);

    return 0;
}

int main()
{
    char* pInStr= "aaaaaaaaacc";
    char pOutStr[1024]= "";
    if (0 == ChangeString(pInStr, pOutStr))
    {
        printf("In : %s\n", pInStr);
        printf("Out : %s\n", pOutStr);
    }
}

第3个回答  2014-01-12
按你说的题意是没法做到的,是不是文字描述不对?“替换为改小写字母在字母表中的下一个字母”,是替换为字母表中下一个字母的大写?
相似回答