进行了一些整理参考:希望大家互相学习,共同进步
https://blog.csdn.net/xll1314521/article/details/75317322
https://blog.csdn.net/shaoyiju/article/details/78377060
一、关于在C++中如何使字符串进行替换的代码
#include <iostream>
#include <string>
using namespace std;
void string_replace( string &strBig, const string &strsrc, const string &strdst)
{
string::size_type pos = 0; //替换字符串的位置
string::size_type srclen = strsrc.size();//替换字符串的长度
string::size_type dstlen = strdst.size();//目的字符串的长度
while( (pos=strBig.find(strsrc, pos)) != string::npos )
{
strBig.replace( pos, srclen, strdst );
pos += dstlen;
}
}
int main(int argc, char*argv[])
{
string strContent = "This is a Test";
string_replace(strContent, "Test", "demo");
cout << strContent << endl;
return 0;
}
二、关于在C语言中进行指定字符进行删除
#include <stdio.h>
char *deleteChar (char *str,char c)
{
char *head = NULL;
char *p = NULL;
if(str ==NULL)
{
return NULL;
}
head = p = str;
while(*p++)
{
if(*p != c)
{
*str++ = *p;
}
}
*str = '\0';
return head;
}
int main()
{
char string[] = "cabcdefcgchci";
char c = 0;
scanf("%c",&c); //输入c
printf("%s\n",string);
deleteChar(string,c);
printf("%s\n",string); //abdefghi
return 0;
}
三、c语言中进行字符右移
#include <stdio.h>
#include <string.h>
#include <assert.h>
//实现字符串的右移两位
void RightLoopMove(char *pStr, unsigned short steps)
{
int i = 0;
int len = strlen(pStr); //字符串的长度
assert(pStr); //判断如果它的条件返回错误,则终止程序执行
for (i = 0; i < steps; i++)
{
char *pend = pStr + len - 1;
char tmp = *(pStr + len - 1);
while (pStr <= pend)
{
*pend = *(pend - 1);
pend--;
}
*pStr = tmp;
}
}
int main()
{
char str[] = "abcdef";
RightLoopMove(str, 2);//进行右移两位
printf("%s\n", str);
getchar();
return 0;
}