修改字符串 两种方法,对形参传过来的

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

  • 例如字符串ATRcccert893#45ae经过处理后应该为ATRdert893#45ae

 

详细描述:

  • 接口说明

原型:

int ChangeString(char *pInStr,char *pOutStr)

输入参数:

char *pInStr 输入字符串

输出参数:

char *pOutStr 输出字符串,内存空间已分配好,可直接使用

返回值:

int 0:处理成功 -1:出现异常

限制:

举例:

pInStr:jkds*^*(HKEEEklIdddjilfff

   pOutStr:jkds*^*(HKEEEklIejilg






该问题需要考虑到多个同样字母的情况,比如


pInStr= "aaaabazzzAs";
pOutStr= "babaaAs";
pInStr= "aaaaaaaaacc";
pOutStr= "d";


解决程序中最重要的是对形参传过来的不要进行拷贝、清零操作,可以新建立一个字符串接收形参,进行操作

c++ 数组清空的方法为:
char a[80];
1、memset() 给指定内存置相应的值
#include <string.h> //引用相关头文件
memset( a, 0, sizeof(a) ); //把从a开始的80个字符全置为0
2、循环遍历数组,逐位清0
for( int i=0;i<sizeof(a);i++ ) 
a[i]=0; //该循环达到memset()函数的效果。
3、所谓的字符串清空:
strcpy(a, ""); 本句相当于 a[0]=0 ;
这里只是完成了数组a的首地址置0,0是字符串结束符'\0'的ascii值,这样赋值后,只是表示一个空字符串,而剩余的数组位置上的数据还是原来存储的内容不会有变化的。所有的字符串操作函数,都会遇0而止的,所以,用户看不到后面位置上的垃圾数据。


方法1:

int ChangeString(char *pInStr,char *pOutStr)
{
	if(NULL == pInStr)
	//if(null== pInStr)//VS2005不识别小写的NULL
		return -1;
	char temp;
	int count = 0;
	int cnt_out = 0;
	int len = strlen(pInStr);
	char *pCur = (char *)malloc(sizeof(char) * (len + 1));//直接对源字符串操作经常出现异常,需要自己建立一个字符串pCur
	strcpy(pCur,pInStr);
	int len_out = 0;
	int i = 0;
	//bool flag = TRUE;//vs2005识别小写的true、false,不识别大写的TRUE、FALSE
	bool flag = true;
	int j = 0;
	//cout<<j<<' '<<flag<<endl;
	while(flag)
	{
		//cout<<j<<endl;
		++j;
	//	memset(pOutStr,0,len_out * sizeof(char));
	//	/*flag = FALSE;*/
		flag = false;
		i = 0;
		cnt_out = 0;
		while(i < len)
		{
			if((pCur[i] >= 'a') && (pCur[i] <= 'z'))
			{
				temp = pCur[i];
				count = 1;
				++i;
				while((temp == pCur[i]) && (count < 3))
				{
					++count;
					++i;
				}
				if(count == 3)
				{
					if('z' == temp)
						pOutStr[cnt_out] = 'a';
					else
						//pOutStr[cnt_out] = (char)((int)temp + 1);
						pOutStr[cnt_out] = ++temp;
					++cnt_out;
					//flag = TRUE;
					flag = true;
				}
				else
				{
					if(count == 1)
					{
						pOutStr[cnt_out] = temp;
						++cnt_out;				
					}
					else
					{
						pOutStr[cnt_out] = temp;
						++cnt_out;
						pOutStr[cnt_out] = temp;
						++cnt_out;
					}
				}
			}
			else
			{
				pOutStr[cnt_out] = pCur[i];
				++cnt_out;
				++i;			
			}
		}
	pOutStr[cnt_out] = '\0';
	//cout<<pOutStr<<endl;
	memset(pInStr,0,len * sizeof(char));//直接对源字符串操作经常出现异常,需要自己建立一个字符串pCur
	//cout<<pInStr<<endl;
	//cout<<pOutStr<<endl;
	//strcpy(pInStr,pOutStr);
	//strncpy(pInStr,pOutStr,(len + 1));
	memset(pCur,0,(len + 1) * sizeof(char));
	memcpy(pCur,pOutStr,cnt_out + 1);
	len_out = cnt_out;
	len = len_out;
	//cout<<pCur<<endl;
	//cout<<pOutStr<<endl;
	//cout<<len<<endl;
	}
	return 0;
}

方法2:

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;//pCur指向了pOut的某个内存区域,所以pCur的变化直接反映到pOutStr中了
            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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值