首页 > 代码库 > 字符串替换

字符串替换

#include <stdlib.h>
#include <string.h>
#include "oj.h"
#include <iostream>
using namespace std;

/*
功能:
    
输入:
    
输出:
     
返回:成功0,其它-1
     
*/

int ProcessString( char * strInput,char chSrc,char chDes ,char * strOutput)
{
    if (NULL == strInput || NULL == strOutput)
    {
        return -1;
    }
    int flag = 0;
    int iCur =0;
    while ('\0' != strInput[iCur])
    {
        if (strInput[iCur] == chSrc)
        {
            strOutput[iCur] = chDes;
            flag = 0;
        }
        else
        {
            strOutput[iCur] = strInput[iCur];
        }
        iCur ++;
    }
    strOutput[iCur] = '\0';
    if (1 == flag)
    {
        return 0;
    }
    else
    {
        return -1;
    }
	
}

int main()
{
    char strInput[] = "d545a";
    char strSrc[] = "d545c";
    char strOutput[10] = "";
    ProcessString(strInput,'a','c',strOutput);
    cout<<strInput<<endl<<strOutput<<endl;
    return 0;
}

字符串替换