Fork me on GitHub

程序实例——查找替换元素

查找替换元素
在s中的元素aabb,s1查找s中是否有相同元素b,有相同元素全部替换为s2c
最后为aacc*/

 1 //查找替换元素
 2 /*在s中的元素aabb,s1查找s中是否有相同元素b,有相同元素全部替换为s2c
 3 最后为aacc*/ 
 4 
 5 #include <iostream>
 6 #define MAX 50
 7 using namespace std;
 8 
 9 void rep(char *s,char *s1,char *s2)
10 {
11     char *p;
12     for(;*s;s++) //顺序访问呢字符串每个元素 
13     {
14         //for(p=s1;*p && *p !=*s;p++)    //除了s1中不替换,其余元素全部替换成s2 
15         for (p = s1; *p&&*p != *s; p++);/*检查当前字符是否在字符串s1中出现*/
16         if(*p)*s=*(p-s1+s2);
17     } 
18 } 
19  int main()
20 {
21     char s[MAX];//="ABCABC";
22     char s1[MAX],s2[MAX];
23 //    clrscr();
24     puts("Please input the string for s:");
25     scanf("%s",s);
26     puts("Please input the string for s1:");
27     scanf("%s",s1);
28     puts("Please input the string for s2:");
29     scanf("%s",s2);
30 
31     rep(s,s1,s2);
32     puts("The string of s after displace is:");
33     printf("%s\n",s);
34     puts("\n Press any key to quit...");
35     return 0;
36 
37 }

 

posted @ 2018-04-17 19:25  风中等待  阅读(186)  评论(0编辑  收藏  举报