实验六

 

问题 A: 去掉空格

 1 #include<stdio.h>
 2 
 3 int main(void)
 4 {
 5    char s[100];
 6    int i;
 7    while(gets(s) != NULL) {
 8       for(i = 0; s[i]; i++) {
 9          if(s[i] != ' ')
10             putchar(s[i]);
11       }
12       puts("");
13    }
14    return 0;
15 }

 

 1 #include<stdio.h>
 2 
 3 char *remove_space(char *s)
 4 {
 5    int i, j;
 6    for(i = j = 0; s[i]; i++){
 7       if(s[i] != ' ')
 8          s[j++] = s[i];
 9    }
10    s[j] = '\0';
11    return s;
12 }
13 
14 int main(void)
15 {
16    char s[100];
17    int i;
18    while(gets(s) != NULL)
19       puts(remove_space(s));
20    return 0;
21 }

 

问题 B: 实验7_6:url分解

 选:作为特征符号

 1 #include<stdio.h>
 2 
 3 int main(void)
 4 {
 5    char s[300];
 6    int i;
 7    while(gets(s) != NULL) {
 8       for(i = 0; s[i] != ':'; i++);
 9       for(i += 3; s[i] != '\0' && s[i] != '/'; i++)
10          putchar(s[i]);
11       printf("\n");
12    }
13    return 0;
14 }

 

如果是www.baidu.com这种数据呢?

 1 #include<stdio.h>
 2 
 3 int main(void)
 4 {
 5    char s[300];
 6    int i;
 7    while(gets(s) != NULL) {
 8       for(i = 0; s[i] != '\0' && s[i] != ':'; i++);
 9       if(s[i] == '\0')
10          i = 0;
11       else
12          i += 3;
13       for(; s[i] != '\0' && s[i] != '/'; i++)
14          putchar(s[i]);
15       printf("\n");
16    }
17    return 0;
18 }

 

问题 C: 去掉双斜杠注释

第13行依赖于文本没有空行

 1 #include<stdio.h>
 2 int main()
 3 {
 4    int i;
 5    char s[200];
 6    while(gets(s) != NULL) {
 7       for(i = 0; s[i] != '\0'; i++) {
 8          if(s[i] == '/' && s[i + 1] == '/'){
 9             s[i] = '\0';
10             break;
11          }
12       }
13       if(i)
14          puts(s);
15    }
16    return 0;
17 }

 

问题 G: 大战ACM

 1 #include <string.h>
 2 #include <stdio.h>
 3 
 4 char *filter(char s[])
 5 {
 6    int i;
 7 
 8    int acm_pos = -1;
 9    for(i = 0; s[i]; i++) {
10       if(s[i] == 'A' && s[i + 1] == 'C' && s[i + 2] == 'M') {
11          acm_pos = i;
12          break;
13       }
14    }
15    if(acm_pos != -1) {
16       //why not use strcpy
17       for(i = acm_pos; s[i] = s[i + 3]; i++)
18          ;
19       filter(s);
20    }
21    return s;
22 }
23 
24 int main(void)
25 {
26    char s[2000];
27 
28    while(scanf("%s", s) != EOF)
29       puts(filter(s));
30 
31    return 0;
32 }

 非递归,一趟扫描

 1 #include <string.h>
 2 #include <stdio.h>
 3 
 4 char *filter(char s[])
 5 {
 6    int i, len = strlen(s);
 7 
 8    int start = 0;
 9    for(start = i = 2 ; i < len; i++) {
10       if(s[start - 2] == 'A' && s[start - 1] == 'C' && s[i] == 'M')
11          start -= 2;
12       else
13          s[start++] = s[i];
14 
15    }
16    s[start] = '\0';
17 
18    return s;
19 }
20 
21 int main(void)
22 {
23    char s[2000];
24 
25    while(scanf("%s", s) != EOF) {
26       puts(filter(s));
27    }
28    
29    return 0;
30 }

 

你可能感兴趣的:(实验六)