对字符串中特殊的通配符进行的解析,然后在重新组合新的字符串输入,这里会用到指针变量
有不对的地方,还请多多指正,这个程序在c_free中进行过验证的,可行且可靠性验证通过
操作字符串的查找与替换,应该还有更优化的算法,还会进一步的改进与优化
自己做的简单的一个算法,比较粗糙,代码如下:
#include<stdio.h>
/*
*function: 查找特定的字符串,并用新的字符串代替,重新组合一个最新字符串
*
*param: buf,传入的原始字符串
* fdValue, 待查找的固定字符串
* value, 需替换的字符串
* newBuf, 返回新的字符串所存储的数组当中
*
*return : 返回新字符串的长度
*/
int findStringReplace(char *buf, const char *fdValue, char *value, char *newBuf)
{
char *pSurIndex = buf;
char *pDstIndex = newBuf;
if (buf == NULL || newBuf == NULL)
{
return 0;
}
while (*pSurIndex != '\0')
{
if (strncasecmp(pSurIndex, fdValue, strlen(fdValue)) == 0)
{
strcpy(pDstIndex, value);
pDstIndex += strlen(value);
pSurIndex += strlen(fdValue);
}
else
{
*(pDstIndex++) = *(pSurIndex++);
}
}
*pDstIndex = '\0';
return strlen(newBuf);
}
int main(int argc, char *argv[])
{
char showcontent[128];
char buf[128];
printf("Please input string:");
gets(buf);
printf("Before replace string is :%s\n\n", buf);
findStringReplace(buf, "$name", "baiyang", showcontent);
printf("After replace string is : %s !\n\n", showcontent);
return 0;
}
有不对的地方,还请多多指正,这个程序在c_free中进行过验证的,可行且可靠性验证通过