将字符串中的字符替换成指定字符c
2017-03-24 19:57
148 查看
C 全部替换字符串里某个字符或者某个子串
/*将字符串中某个字符全部替换为另一个字符*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intsubstitude(char s1[],char A,char B)
{ int N,P=0; N= strlen(s1); for (int M = 0;M <= N;M++) { if (s1[M] == A)
{ s1[M] = B;
P=P+1; } }
return P; }
int main()
{ printf("欢迎使用替换字符的程序\n");
char s2[81];
char E, D;
printf("请输入一个字符串:"); scanf("%81s",s2);
printf("\n请输入被替换的字符和替换后的字符:");
scanf("%s,%s",&E,&D);
printf("需要的次数为:",E,D);
printf("%d",substitude(s2,E,D));
system("PAUSE");
return 0;
}
https://wenku.baidu.com/view/b3d2fc0d0c22590103029da5.html https://zhidao.baidu.com/question/355521750.html
{
int i = 0;
char *q = NULL;
q = source;
for(i=0; i<strlen(q); i++)
{
if(q[i] == s1)
{
q[i] = s2;
}
}
strcpy(result, q);
}
void replace_string(char *result, char *source, char* s1, char *s2)
{
char *q=NULL;
char *p=NULL;
p=source;
while((q=strstr(p, s1))!=NULL)
{
strncpy(result, p, q-p);
result[q-p]= '\0';//very important, must attention!
strcat(result, s2);
strcat(result, q+strlen(s1));
strcpy(p,result);
}
strcpy(result, p);
}
void main()
{
char url[64] = "ad12 fdf5 d12f dfd12 dfp";
char result[64] = {0};
replace_char(result, url, ' ', '_' );//把url里所有‘ ’字符替换成'_'字符,结果存在result中
printf("char final result=%s\n",result);
memset(result, 0, sizeof(result));
replace_string(result, url, "12", "@@@");//把url里所有“12”字符串替换成“@@@”字符串
printf("string final result=%s\n", result);
}分别单独运行的结果如下:测试结果:char final result=ad12_fdf5_d12f_dfd12_dfp测试结果:string:final result=ad@@@ fdf5 d@@@f dfd@@@ dfp
/*将字符串中某个字符全部替换为另一个字符*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intsubstitude(char s1[],char A,char B)
{ int N,P=0; N= strlen(s1); for (int M = 0;M <= N;M++) { if (s1[M] == A)
{ s1[M] = B;
P=P+1; } }
return P; }
int main()
{ printf("欢迎使用替换字符的程序\n");
char s2[81];
char E, D;
printf("请输入一个字符串:"); scanf("%81s",s2);
printf("\n请输入被替换的字符和替换后的字符:");
scanf("%s,%s",&E,&D);
printf("需要的次数为:",E,D);
printf("%d",substitude(s2,E,D));
system("PAUSE");
return 0;
}
https://wenku.baidu.com/view/b3d2fc0d0c22590103029da5.html https://zhidao.baidu.com/question/355521750.html
void rep(char *s,char *s1,char *s2) { char *p; for(;*s;s++) /*顺序访问字符串s中的每个字符*/ { for(p=s1;*p&&*p!=*s;p++);/*检查当前字符是否在字符串s1中出现*/ if(*p) *s=*(p-s1+s2); /*当前字符在字符串s1中出现,用字符串s2中的对应字符代替s中的字符*/ } }
http://blog.csdn.net/ifen_huang/article/details/52957863
#include<string.h> #include<stdio.h>#include<stdlib.h>//把source字符串里所有s1字符全部替换成字符s2void replace_char(char *result, char *source, char s1, char s2)
{
int i = 0;
char *q = NULL;
q = source;
for(i=0; i<strlen(q); i++)
{
if(q[i] == s1)
{
q[i] = s2;
}
}
strcpy(result, q);
}
void replace_string(char *result, char *source, char* s1, char *s2)
{
char *q=NULL;
char *p=NULL;
p=source;
while((q=strstr(p, s1))!=NULL)
{
strncpy(result, p, q-p);
result[q-p]= '\0';//very important, must attention!
strcat(result, s2);
strcat(result, q+strlen(s1));
strcpy(p,result);
}
strcpy(result, p);
}
void main()
{
char url[64] = "ad12 fdf5 d12f dfd12 dfp";
char result[64] = {0};
replace_char(result, url, ' ', '_' );//把url里所有‘ ’字符替换成'_'字符,结果存在result中
printf("char final result=%s\n",result);
memset(result, 0, sizeof(result));
replace_string(result, url, "12", "@@@");//把url里所有“12”字符串替换成“@@@”字符串
printf("string final result=%s\n", result);
}分别单独运行的结果如下:测试结果:char final result=ad12_fdf5_d12f_dfd12_dfp测试结果:string:final result=ad@@@ fdf5 d@@@f dfd@@@ dfp
相关文章推荐
- C# 关于字符串中 符合正则表达式的指定字符的替换的方法
- 替换字符串中指定的字符--随手源码
- js替换字符串中所有指定的字符(实现代码)
- 5.6 替换字符串中连续出现的指定字符串
- js replace(a,b)之替换字符串中所有指定字符的方法
- 将字符串中的指定字符替换为其他指定字符
- HW—指定字符替换Java(replace)---动态和静态定义字符串数组;
- js replace(a,b)之替换字符串中所有指定字符的方法
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 字符串指定位置替换字符
- 22.c++-替换字符串中指定的字符(随手源码)
- string 将指定的字符或字符串全部替换成特定的字符或字符串
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- js replace(a,b)之替换字符串中所有指定字符的方法
- 利用替换字符串的函数StringReplace删除字符串中指定的字符或字符串
- 项目--js中替换目标字符串指定的所有字符
- 今天学到的几个替换字符串中指定字符方法。
- js替换字符串中所有指定的字符
- java判断字符串中某字符出现的位置,并替换指定指定内容
- js替换字符串中所有指定的字符(实现代码)