格式字符串的输入输出

22号去参加华为的武汉实习生上机考试,3道题目,当时就做出来两道,有一道格式字符串的题目没有做出来。回到学校之后还是重新想了想,把当时没做出来的再做一遍。

原题在华为的题库中也没找到,我就凭自己的记忆重新写个大意一样的题目了。

题目差不多是这样的:

有一个格式化的字符串,如下所示:

name=Jorden,job=palyer,age=45

编写代码,可以识别以上的格式,如果输入格式不对,会报错。同时输出格式如下:

[[name,Jorden],[job,player],[age,45]]

其实现在想想这里给的信息也不是很全啊,没有说允不允许空输入的存在。(不知道是不是我忘了点题目的内容 = =)。例如:

name=Jorden,job=,age=45

job为空,输入格式符合不?我是忽略这种情况了。按我的理解就是把

'='',' 

 当作关键字符了,如果输多了、少了或者输入的顺序不对就报错了。

以下是我的代码:

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 
  4 #define true 1
  5 #define false 0
  6 
  7 //获得字符串长度
  8 int getlen(char *s)
  9 {
 10     int i;
 11     for (i=0; s[i]!=''; i++) {
 12         ;
 13     }
 14     return i;
 15 }
 16 //获得特定字符在字符串内的个数
 17 int num_char(char *s, char flg)
 18 {
 19     int i, count = 0;
 20     for (i=0; s[i]!=''; i++) {
 21         if (s[i] == flg) {
 22             count++;
 23         }
 24     }
 25     return count;
 26 }
 27 //获得特定字符在字符串内的位置,返回p
 28 void find_char(char *s, char flg, int *p)
 29 {
 30     int i, index = 0;
 31     for (i=0; s[i]!=''; i++) {
 32         if (s[i] == flg) {
 33             p[index++] = i;
 34         }
 35     }
 36 }
 37 
 38 int main(void)
 39 {
 40     char s[256], out[256] = "[[";
 41     int count_c, count_e, count_t;
 42     int *p_c, *p_e, *p_t;
 43     int i, j, e=0, c=0, flg, n=2;
 44     scanf("%s", s);
 45 
 46     count_c = num_char(s, ',');
 47     count_e = num_char(s, '=');
 48 
 49     //判断逗号个数跟等号个数的关系
 50     if ((count_e < 1) || (count_c != count_e - 1)) {
 51         printf("input error");
 52         return 1;
 53     }
 54     //p_c逗号的位置,p_e等号的位置
 55     p_c = (int *)(malloc(sizeof(int) * count_c));
 56     p_e = (int *)(malloc(sizeof(int) * count_e));
 57 
 58     find_char(s, ',', p_c);
 59     find_char(s, '=', p_e);
 60     //等号在逗号之前,同时最后一个等号在最后一个逗号之后
 61     //其实这里默认允许空内容的存在了。
 62     for (i=0; i<count_c; i++) {
 63         if (p_c[i] < p_e[i]) {
 64             printf("input error");
 65             return 1;
 66         }
 67     }
 68     if (p_e[count_e-1] < p_c[count_c-1]) {
 69         printf("input error");
 70             return 1;
 71     }
 72 
 73     //把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度
 74     count_t = count_c + count_e + 2;
 75     p_t = (int *)(malloc(sizeof(int) * (count_t)));
 76     p_t[0] = -1;
 77     flg = true;
 78     for(i=1; i<count_t-1; i++) {
 79         if (flg == true) {
 80             p_t[i] = p_e[e++];
 81         } else {
 82             p_t[i] = p_c[c++];
 83         }
 84         flg = !flg;
 85     }
 86     p_t[count_t - 1] = getlen(s);
 87 
 88     free(p_c);
 89     free(p_e);
 90 
 91     //把位置里的字符串移到out字符串内
 92     flg = true;
 93     for (i=0; i<count_t-1; i++) {
 94         for (j=p_t[i]+1; j<p_t[i+1]; j++) {
 95             out[n++] = s[j];
 96         }
 97         if (flg == true) {
 98             out[n++] = ',';
 99         } else {
100             out[n++] = ']';
101             out[n++] = ',';
102             out[n++] = '[';
103         }
104         flg = !flg;
105     }
106     //收尾处理
107     n=n-2;
108     out[n++] = ']';
109     out[n++] = '';
110 
111     free(p_t);
112     printf("%s", out);
113 
114     system("pause");
115     return 0;
116 }

以下是禁止了输入为空的:

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 
  4 #define true 1
  5 #define false 0
  6 
  7 //获得字符串长度
  8 int getlen(char *s)
  9 {
 10     int i;
 11     for (i=0; s[i]!=''; i++) {
 12         ;
 13     }
 14     return i;
 15 }
 16 //获得特定字符在字符串内的个数
 17 int num_char(char *s, char flg)
 18 {
 19     int i, count = 0;
 20     for (i=0; s[i]!=''; i++) {
 21         if (s[i] == flg) {
 22             count++;
 23         }
 24     }
 25     return count;
 26 }
 27 //获得特定字符在字符串内的位置,返回p
 28 void find_char(char *s, char flg, int *p)
 29 {
 30     int i, index = 0;
 31     for (i=0; s[i]!=''; i++) {
 32         if (s[i] == flg) {
 33             p[index++] = i;
 34         }
 35     }
 36 }
 37 
 38 int main(void)
 39 {
 40     char s[256], out[256] = "[[";
 41     int count_c, count_e, count_t;
 42     int *p_c, *p_e, *p_t;
 43     int i, j, e=0, c=0, flg, n=2;
 44     scanf("%s", s);
 45 
 46     count_c = num_char(s, ',');
 47     count_e = num_char(s, '=');
 48 
 49     //判断逗号个数跟等号个数的关系
 50     if ((count_e < 1) || (count_c != count_e - 1)) {
 51         printf("input error");
 52         return 1;
 53     }
 54     //p_c逗号的位置,p_e等号的位置
 55     p_c = (int *)(malloc(sizeof(int) * count_c));
 56     p_e = (int *)(malloc(sizeof(int) * count_e));
 57 
 58     find_char(s, ',', p_c);
 59     find_char(s, '=', p_e);
 60     //等号在逗号之前,同时最后一个等号在最后一个逗号之后
 61     //这里的判断变了。
 62     if ((p_e[0] == 0) || (p_e[count_e - 1] == (getlen(s) - 1))) {
 63         printf("input error");
 64             return 1;
 65     }
 66 
 67     for (i=0; i<count_c; i++) {
 68         if (p_c[i] <= p_e[i] + 1) {
 69             printf("input error");
 70             return 1;
 71         }
 72     }
 73     if (p_e[count_e-1] <= p_c[count_c-1] + 1) {
 74         printf("input error");
 75             return 1;
 76     }
 77 
 78 
 79     //把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度
 80     count_t = count_c + count_e + 2;
 81     p_t = (int *)(malloc(sizeof(int) * (count_t)));
 82     p_t[0] = -1;
 83     flg = true;
 84     for(i=1; i<count_t-1; i++) {
 85         if (flg == true) {
 86             p_t[i] = p_e[e++];
 87         } else {
 88             p_t[i] = p_c[c++];
 89         }
 90         flg = !flg;
 91     }
 92     p_t[count_t - 1] = getlen(s);
 93 
 94     free(p_c);
 95     free(p_e);
 96 
 97     //把位置里的字符串移到out字符串内
 98     flg = true;
 99     for (i=0; i<count_t-1; i++) {
100         for (j=p_t[i]+1; j<p_t[i+1]; j++) {
101             out[n++] = s[j];
102         }
103         if (flg == true) {
104             out[n++] = ',';
105         } else {
106             out[n++] = ']';
107             out[n++] = ',';
108             out[n++] = '[';
109         }
110         flg = !flg;
111     }
112     //收尾处理
113     n=n-2;
114     out[n++] = ']';
115     out[n++] = '';
116 
117     free(p_t);
118     printf("%s", out);
119 
120     system("pause");
121     return 0;
122 }

如果代码有错,多谢指点。

原文地址:https://www.cnblogs.com/tqianly/p/4456445.html