pat1009,字符串的输入处理
2014-02-24 09:09
417 查看
地址:http://pat.zju.edu.cn/contests/pat-b-practise/1009
本题中遇到的问题:
scanf("%s%c", buf, c);
忘记&,运行时:XX已停止工作。调试错误:非法访问。
修改后,scanf("%s%c", buf, &c);运行正确:
另外一种方法,1个字符一个字符读入处理。掌握这种方法是非常有必要的。
需要注意的事项:
字符串读入结束后需要加结束标记'\0',如果忘记,调试时,可以看到:
当然程序运行时也会错误:输出时直接出现乱码,因为没有添加字符串的借宿标识符。
AC代码:
本题中遇到的问题:
scanf("%s%c", buf, c);
忘记&,运行时:XX已停止工作。调试错误:非法访问。
修改后,scanf("%s%c", buf, &c);运行正确:
#include<stdio.h> int main(){ #ifdef ONLINE_JUDGE #else freopen("E:\\in.txt", "r", stdin); #endif char sen[80][100]; int cn=0; char c; while(scanf("%s%c", sen[cn++], &c) != EOF){ if(c == '\n'){ printf("%s\n", sen[--cn]); continue; } while(scanf("%s%c", sen[cn++], &c) != EOF){ if (c == '\n') break; } while(--cn >0){ printf("%s ", sen[cn]); } printf("%s\n", sen[cn]); } return 0; }
另外一种方法,1个字符一个字符读入处理。掌握这种方法是非常有必要的。
需要注意的事项:
字符串读入结束后需要加结束标记'\0',如果忘记,调试时,可以看到:
当然程序运行时也会错误:输出时直接出现乱码,因为没有添加字符串的借宿标识符。
AC代码:
#include<stdio.h> #include<string.h> int main(){ #ifdef ONLINE_JUDGE #else freopen("E:\\in.txt", "r", stdin); freopen("E:\\out.txt", "w", stdout); #endif char sen[80][100], buf[100]; int cn=0; char c; while(scanf("%c", &c) != EOF){ if(c == '\n'){ continue; }// skip blank //first char not new line int len=0; buf[len++]=c; while(scanf("%c", &c) != EOF){ if(c == '\n'){ buf[len]='\0'; strcpy(sen[cn++], buf);//end of a word, save len=0; break; // end of a line } if(c == ' '){ buf[len]='\0';//add \0 strcpy(sen[cn++], buf);//end of a word, save len=0; continue;// next word } buf[len++]=c; }//end of reading a line while(--cn >0){ printf("%s ", sen[cn]); } printf("%s\n", sen[cn]); cn=0; } return 0; }
相关文章推荐
- PAT-B] 1009. 说反话 [字符串处理]
- PAT-A1082.Read Number in Chinese 【字符串处理】
- C++错误输入的处理--整型错误输入字符串
- ytu 1064: 输入三个字符串,按由小到大的顺序输出(水题,字符串处理)
- C++中string类的处理字符串构造函数用法, 以及getline cin.getline()输入的区别
- 描述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理 随后的n行,每行有一个字符串,该字符串的长度不
- 字符串处理系列:输入任何一串字符串,计算其中ABC子字符串的个数
- python下mysql数据库处理输入字符串
- ZJU_PAT_basic level_1014 字符串的简单处理与临界值的判定
- PAT-B] 1002. 写出这个数 [字符串处理]
- zjfc----1076 online judge 对多行字符串输入做处理
- •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
- PAT 天梯赛 L1-039. 古风排版 【字符串处理】
- PAT - 甲级 - 1001. A+B Format (20)(字符串处理)
- PAT - 甲级 - 1077. Kuchiguse (20)(字符串处理)
- pat1081 学习下strtok_r()对分隔字符串的处理(当然不用也行,scanf读取时加控制符)
- PAT-L1古风排版 打印字符串和处理
- 【PAT】B1009. 说反话(C++接受带有空格的字符串)
- PAT-B] 1014. 福尔摩斯的约会 [字符串处理]
- poj 2263 Heavy Cargo floyd基础,就是输入的时候处理字符串纠结一点!!!!