把一个字符串中特定的字符全部用给定的字符替换,得到一个新的字符串。

来源:互联网 发布:linux文件强制锁 编辑:程序博客网 时间:2024/06/08 03:16
试题描述
把一个字符串中特定的字符全部用给定的字符替换,得到一个新的字符串。
输入格式
只有一行,由一个字符串和两个字符组成,中间用单个空格隔开。字符串是待替换的字符串,字符串长度小于等于30个字符,且不含空格等空白符;接下来一个字符为需要被替换的特定字符;接下来一个字符为用于替换的给定字符。
输出格式
一行,即替换后的字符串。
样例输入
hello-how-are-you o O
样例输出

hellO-hOw-are-yOu

#include<stdio.h>#include<string.h>int main(){    char s[30];//输入一个字符串s    char a[2],b[2];//用两个字符串a,b的第一位存储字符    while(scanf("%s%s%s", s, a, b)!=EOF)    {        int len = strlen(s);        int i;        change(s,a,b);        puts("");    }    return 0;}void change(char *s,char *a,char *b){    int i,len=0;    len=strlen(s);    for(i = 0; i < len; i++) printf("%c", s[i] == a[0] ? b[0] : s[i]);    //当s[i]==a[0]时,用b[0]替换a[0],输出替换后的字符串s}
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. #define N 3333
  5. char s[N], a[2], b[2];
  6. int main()
  7. {
  8. while(scanf("%s%s%s", s, a, b)!=EOF)
  9. {
  10. int len = strlen(s);
  11. int i;
  12. for(i = 0; i < len; i++) printf("%c", s[i] == a[0] ? b[0] : s[i]);
  13. puts("");
  14. }
  15. return 0;
  16. }




阅读全文
0 0