关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理

一、删除前缀 '*'

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*p; //字符串缓冲区;字符串头指针;字符串临时指针 
10     int chr_num=0,b_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数
11     int i;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //统计输入的字符串中前缀 * 的个数
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*')
27     {
28         b_num++;
29     }
30     
31     //删除输入的字符串中的前缀 *  
32     for(i=0;i<chr_num-b_num;i++)
33     {
34         chr[i]=chr[i+b_num];
35     }
36     chr[i]='';
37     
38     //输出
39     cout<<"The result:"<<endl;
40     puts(chr);
41     
42     return 0; 
43 }
View Code

二、删除后缀 '*'

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*t,*p; //字符串缓冲区;字符串尾指针;字符串临时指针 
10     int chr_num=0,t_num=0; //输入的字符串中字符的个数;输入的字符串中后缀 * 的个数 
11     
12     //输入 
13     cout<<"Please input a string:"<<endl; 
14     gets(chr);
15     
16     //统计输入的字符串中字符的个数 
17     p=chr; // p 指向字符串的第一个字符 
18     while(*p++)
19     {
20         chr_num++;
21     }    
22     
23     //统计输入的字符串中后缀 * 的个数 
24     t=chr+chr_num-1; // t 指向字符串的最后一个字符 
25     while(*t--=='*')
26     {
27         t_num++;
28     }
29     
30     //删除输入的字符串中的后缀 *  
31     chr[chr_num-t_num]='';
32     
33     //输出
34     cout<<"The result:"<<endl;
35     puts(chr);
36     
37     return 0; 
38 } 
View Code

三、删除前缀和后缀 '*'

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*t,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0,b_num=0,t_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数;输入的字符串中后缀 * 的个数 
11     int i;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //统计输入的字符串中前缀 * 的个数
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*')
27     {
28         b_num++;
29     }    
30     
31     //统计输入的字符串中后缀 * 的个数 
32     t=chr+chr_num-1; // t 指向字符串的最后一个字符 
33     while(*t--=='*')
34     {
35         t_num++;
36     }
37     
38     //删除输入的字符串中的前缀和后缀 *  
39     for(i=0;i<chr_num-b_num-t_num;i++)
40     {
41         chr[i]=chr[i+b_num];
42     }
43     chr[i]='';
44 
45     //输出
46     cout<<"The result:"<<endl;
47     puts(chr);
48     
49     return 0; 
50 } 
View Code

四、删除中间 '*'

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*t,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数;输入的字符串中后缀 * 的个数 
11     int i;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //令 b 指向字符串中前缀 * 之后的第一个字符 
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*'); 
27 
28     //令 t 指向字符串中后缀 * 之前的第一个字符 
29     t=chr+chr_num-1; // t 指向字符串的最后一个字符 
30     while(*t--=='*');
31     
32     //删除字符串中间的 *  
33     p=b; //令 p 指向字符串中前缀 * 之后的第一个字符 
34     while(b<=t)
35     {
36         if(*b!='*')
37         {
38             *p=*b;
39             p++;
40             b++;
41         }
42         else
43         {
44             b++;
45         }
46     }
47     ++t; //令 t 指向字符串中后缀 * 的第一个 * 
48     while(*t)
49     {
50         *p=*t;
51         p++;
52         t++;
53     }
54     *p=''; 
55     
56     //输出
57     cout<<"The result:"<<endl;
58     puts(chr);
59     
60     return 0; 
61 } 
View Code

五、把字符串中的前缀 * 移动到字符串的末尾 

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0,b_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数
11     int i;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //统计输入的字符串中前缀 * 的个数
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*')
27     {
28         b_num++;
29     }
30     
31     //把字符串中的前缀 * 移动到字符串的末尾 
32     for(i=0;i<chr_num-b_num;i++) //删除输入的字符串中的前缀 *  
33     {
34         chr[i]=chr[i+b_num];
35     }
36     while(i<chr_num) //在字符串的末尾添加与前缀个数相同的 * 
37     {
38         chr[i++]='*';
39     }
40     chr[i]='';
41     
42     //输出
43     cout<<"The result:"<<endl;
44     puts(chr);
45     
46     return 0; 
47 } 
View Code

六、把字符串中的后缀 * 移动到字符串的开头 

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*t,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0,b_num=0,t_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数;输入的字符串中后缀 * 的个数 
11     int i;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //统计输入的字符串中前缀 * 的个数
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*')
27     {
28         b_num++;
29     }    
30     
31     //统计输入的字符串中后缀 * 的个数 
32     t=chr+chr_num-1; // t 指向字符串的最后一个字符 
33     while(*t--=='*')
34     {
35         t_num++;
36     }
37     
38     //把字符串中的后缀 * 移动到字符串的开头 
39     for(i=chr_num-t_num-1;i>=0;i--) //删除输入的字符串中的后缀 * 
40     {
41         chr[i+t_num]=chr[i];
42     }
43     for(i=0;i<t_num;i++) //在字符串的开头添加与后缀个数相同的 * 
44     {
45         chr[i]='*';
46     }
47     
48     //输出
49     cout<<"The result:"<<endl;
50     puts(chr);
51     
52     return 0; 
53 } 
View Code

七、使字符串的前缀 * 不多于 n 个

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*b,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0,b_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数;输入的字符串中后缀 * 的个数 
11     int i,n=5;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23 
24     //统计输入的字符串中前缀 * 的个数
25     b=chr; // b 指向字符串的第一个字符 
26     while(*b++=='*')
27     {
28         b_num++;
29     }
30     
31     //使字符串的前缀 * 不多于 n 个
32     if(b_num>n)
33     {
34         //删除输入的字符串中的前缀和后缀 *  
35         for(i=0;i<chr_num-b_num;i++)
36         {
37             chr[i+n]=chr[i+b_num];
38         }
39         chr[i+n]='';
40     }
41     
42     //输出
43     cout<<"The result:"<<endl;
44     puts(chr);
45     
46     return 0; 
47 } 
View Code

八、使字符串的后缀 * 不多于 n 个

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 //主函数
 7 int main()
 8 {
 9     char chr[20],*t,*p; //字符串缓冲区;字符串头指针;字符串尾指针;字符串临时指针 
10     int chr_num=0,t_num=0; //输入的字符串中字符的个数;输入的字符串中前缀 * 的个数;输入的字符串中后缀 * 的个数 
11     int i,n=5;
12     
13     //输入 
14     cout<<"Please input a string:"<<endl; 
15     gets(chr);
16     
17     //统计输入的字符串中字符的个数 
18     p=chr; // p 指向字符串的第一个字符 
19     while(*p++)
20     {
21         chr_num++;
22     }
23     
24     //统计输入的字符串中后缀 * 的个数 
25     t=chr+chr_num-1; // t 指向字符串的最后一个字符 
26     while(*t--=='*')
27     {
28         t_num++;
29     }
30     
31     //使字符串的后缀 * 不多于 n 个
32     if(t_num>n)
33     {
34         chr[chr_num-t_num+n]='';
35     }
36 
37     //输出
38     cout<<"The result:"<<endl;
39     puts(chr);
40     
41     return 0; 
42 } 
View Code
原文地址:https://www.cnblogs.com/LeoFeng/p/5349741.html