格式字符串的输入输出
2015-04-25 19:27
28 查看
22号去参加华为的武汉实习生上机考试,3道题目,当时就做出来两道,有一道格式字符串的题目没有做出来。回到学校之后还是重新想了想,把当时没做出来的再做一遍。
原题在华为的题库中也没找到,我就凭自己的记忆重新写个大意一样的题目了。
题目差不多是这样的:
有一个格式化的字符串,如下所示:
编写代码,可以识别以上的格式,如果输入格式不对,会报错。同时输出格式如下:
其实现在想想这里给的信息也不是很全啊,没有说允不允许空输入的存在。(不知道是不是我忘了点题目的内容 = =)。例如:
job为空,输入格式符合不?我是忽略这种情况了。按我的理解就是把
当作关键字符了,如果输多了、少了或者输入的顺序不对就报错了。
以下是我的代码:
以下是禁止了输入为空的:
如果代码有错,多谢指点。
原题在华为的题库中也没找到,我就凭自己的记忆重新写个大意一样的题目了。
题目差不多是这样的:
有一个格式化的字符串,如下所示:
name=Jorden,job=palyer,age=45
编写代码,可以识别以上的格式,如果输入格式不对,会报错。同时输出格式如下:
[[name,Jorden],[job,player],[age,45]]
其实现在想想这里给的信息也不是很全啊,没有说允不允许空输入的存在。(不知道是不是我忘了点题目的内容 = =)。例如:
name=Jorden,job=,age=45
job为空,输入格式符合不?我是忽略这种情况了。按我的理解就是把
'=' 和 ','
当作关键字符了,如果输多了、少了或者输入的顺序不对就报错了。
以下是我的代码:
#include<stdio.h> #include<malloc.h> #define true 1 #define false 0 //获得字符串长度 int getlen(char *s) { int i; for (i=0; s[i]!='\0'; i++) { ; } return i; } //获得特定字符在字符串内的个数 int num_char(char *s, char flg) { int i, count = 0; for (i=0; s[i]!='\0'; i++) { if (s[i] == flg) { count++; } } return count; } //获得特定字符在字符串内的位置,返回p void find_char(char *s, char flg, int *p) { int i, index = 0; for (i=0; s[i]!='\0'; i++) { if (s[i] == flg) { p[index++] = i; } } } int main(void) { char s[256], out[256] = "[["; int count_c, count_e, count_t; int *p_c, *p_e, *p_t; int i, j, e=0, c=0, flg, n=2; scanf("%s", s); count_c = num_char(s, ','); count_e = num_char(s, '='); //判断逗号个数跟等号个数的关系 if ((count_e < 1) || (count_c != count_e - 1)) { printf("input error"); return 1; } //p_c逗号的位置,p_e等号的位置 p_c = (int *)(malloc(sizeof(int) * count_c)); p_e = (int *)(malloc(sizeof(int) * count_e)); find_char(s, ',', p_c); find_char(s, '=', p_e); //等号在逗号之前,同时最后一个等号在最后一个逗号之后 //其实这里默认允许空内容的存在了。 for (i=0; i<count_c; i++) { if (p_c[i] < p_e[i]) { printf("input error"); return 1; } } if (p_e[count_e-1] < p_c[count_c-1]) { printf("input error"); return 1; } //把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度 count_t = count_c + count_e + 2; p_t = (int *)(malloc(sizeof(int) * (count_t))); p_t[0] = -1; flg = true; for(i=1; i<count_t-1; i++) { if (flg == true) { p_t[i] = p_e[e++]; } else { p_t[i] = p_c[c++]; } flg = !flg; } p_t[count_t - 1] = getlen(s); free(p_c); free(p_e); //把位置里的字符串移到out字符串内 flg = true; for (i=0; i<count_t-1; i++) { for (j=p_t[i]+1; j<p_t[i+1]; j++) { out[n++] = s[j]; } if (flg == true) { out[n++] = ','; } else { out[n++] = ']'; out[n++] = ','; out[n++] = '['; } flg = !flg; } //收尾处理 n=n-2; out[n++] = ']'; out[n++] = '\0'; free(p_t); printf("%s", out); system("pause"); return 0; }
以下是禁止了输入为空的:
#include<stdio.h> #include<malloc.h> #define true 1 #define false 0 //获得字符串长度 int getlen(char *s) { int i; for (i=0; s[i]!='\0'; i++) { ; } return i; } //获得特定字符在字符串内的个数 int num_char(char *s, char flg) { int i, count = 0; for (i=0; s[i]!='\0'; i++) { if (s[i] == flg) { count++; } } return count; } //获得特定字符在字符串内的位置,返回p void find_char(char *s, char flg, int *p) { int i, index = 0; for (i=0; s[i]!='\0'; i++) { if (s[i] == flg) { p[index++] = i; } } } int main(void) { char s[256], out[256] = "[["; int count_c, count_e, count_t; int *p_c, *p_e, *p_t; int i, j, e=0, c=0, flg, n=2; scanf("%s", s); count_c = num_char(s, ','); count_e = num_char(s, '='); //判断逗号个数跟等号个数的关系 if ((count_e < 1) || (count_c != count_e - 1)) { printf("input error"); return 1; } //p_c逗号的位置,p_e等号的位置 p_c = (int *)(malloc(sizeof(int) * count_c)); p_e = (int *)(malloc(sizeof(int) * count_e)); find_char(s, ',', p_c); find_char(s, '=', p_e); //等号在逗号之前,同时最后一个等号在最后一个逗号之后 //这里的判断变了。 if ((p_e[0] == 0) || (p_e[count_e - 1] == (getlen(s) - 1))) { printf("input error"); return 1; } for (i=0; i<count_c; i++) { if (p_c[i] <= p_e[i] + 1) { printf("input error"); return 1; } } if (p_e[count_e-1] <= p_c[count_c-1] + 1) { printf("input error"); return 1; } //把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度 count_t = count_c + count_e + 2; p_t = (int *)(malloc(sizeof(int) * (count_t))); p_t[0] = -1; flg = true; for(i=1; i<count_t-1; i++) { if (flg == true) { p_t[i] = p_e[e++]; } else { p_t[i] = p_c[c++]; } flg = !flg; } p_t[count_t - 1] = getlen(s); free(p_c); free(p_e); //把位置里的字符串移到out字符串内 flg = true; for (i=0; i<count_t-1; i++) { for (j=p_t[i]+1; j<p_t[i+1]; j++) { out[n++] = s[j]; } if (flg == true) { out[n++] = ','; } else { out[n++] = ']'; out[n++] = ','; out[n++] = '['; } flg = !flg; } //收尾处理 n=n-2; out[n++] = ']'; out[n++] = '\0'; free(p_t); printf("%s", out); system("pause"); return 0; }
如果代码有错,多谢指点。
相关文章推荐
- 按照要求的输入输出格式进行日期字符串的转化
- C语言从认识到熟悉 - 3(格式字符串输入输出)
- C#判断字符串是否为日期格式
- C#字符串去除html格式
- oracle学习笔记【 ORA-01861: literal does not match format string(字符串格式不匹配)】
- C#使用String.Format()方法时,出现异常"输入的字符串格式有误"的解决方法
- Java Json格式的字符串转变对象
- 在IE10中点击updatepanel里面的imagebutton时出现的错误:输入字符串的格式不正确( 在 System.Number.StringToNumber)
- 将json格式的字符串解析成Map对象
- C语言INT64类型和GCC中的long long的定义,及sprintf格式字符串的定义
- Beginning Python From Novice to Professional (4) - 字符串格式示例
- iOS 在聊天界面通过时间戳得到标准格式的字符串
- JS 字符串格式日期转日期格式
- 特殊需求:EF 6.x如何比较TimeSpan格式的字符串?EF Core实现方式是否和EF 6.x等同?
- javascript中将json格式字符串转换成json对象实例
- C# 自定义类型通过实现IFormattable接口,来输出指定的格式和语言文化的字符串(例:DateTime)
- 从ini格式文件中(没行是“键=值”格式)中读取配置项的值,Split字符串的处理(综合)
- 检测字符串编码格式
- js字符串转为日期格式
- 输入输出--数值转换为字符串