心得之gets
来源:互联网 发布:最优化方法孙文瑜辅导 编辑:程序博客网 时间:2024/05/19 03:22
说来实在是惭愧,学习acm也有将近一年了,虽说大多数时间都没干实事,但是连gets都不会用,简直不知道自己到底在干什么。。。
百科定义:
gets函数,可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大。
gets从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为NUL值,并由此来结束字符串。
头文件:stdio.h(c),cstdio(c++)
原型:char*gets(char*);
以前做法:
以前不知道用gets,所以总是自己写函数来读入
#include <cstdio>//getchar头文件#include <conio.h>//getch头文件char String[105];int tot;void getstring(){ char ch; tot=0; while(1) { ch=getchar(); if(ch=='\n'||ch==EOF) { /*下面的注释是代表用getch不用getchar,但是getchar函数中键入ctrl+z可以得到EOF值,而getch函数直接读入键盘的输入,不走缓冲区,得不到EOF所以我超时了几次*/ //printf("\n"); return; } if(ch=='\b') { //printf("%c %c",ch,ch); tot--; continue; } //printf("%c",ch); String[tot++]=ch; }}
现在只要一个gets(String);
注意事项:
如果gets函数的前面还输入了其他东西,就要注意可能回车会留在键盘缓存区里,所以我就想了个傻办法,至于正宗方法还请大神赐教
char ch;int n;while(scanf("%d",&n)!=EOF){ scanf("%c",&ch);//用于消除缓存区里的回车 while(n--) { getstring(); }}
例题:
1、Palindromes _easy version-hdu2029
题意:输入n个字符串,判断是否为回文串
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;bool isPalindrome(char String[]){ int len=strlen(String); int i,j; for(i=0,j=len-1;i<=j;i++,j--) { if(String[i]!=String[j]) return false; } return true;}int main(){ char String[105],ch; int n; while(cin>>n) { scanf("%c",&ch);//用于消除回车 while(n--) { gets(String); if(isPalindrome(String)) cout<<"yes"<<endl; else cout<<"no"<<endl; } }}
2、统计元音-hdu2027
题意:统计字符串中元音个数,但是多组数据答案要最后一起输出
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main(){ char String[100]; char ch; int n; int num[105][105]={0}; scanf("%d",&n); scanf("%c",&ch); for(int i=0;i<n;i++) { gets(String); for(int j=0;String[j];j++) { switch(String[j]) { case 'a': num[i][1]++;break; case 'e': num[i][2]++;break; case 'i': num[i][3]++;break; case 'o': num[i][4]++;break; case 'u': num[i][5]++;break; default: break; } //printf("%d %c",num[i][1],String[j]); } } for(int i=0;i<n;i++) { printf("a:%d\n",num[i][1]); printf("e:%d\n",num[i][2]); printf("i:%d\n",num[i][3]); printf("o:%d\n",num[i][4]); printf("u:%d\n",num[i][5]); if(i!=n-1) printf("\n"); } return 0;}
3、C语言合法标识符-hdu2024
题意:判断字符串是否是c的合法标识符(即由字母、下划线、数字这三个方面组成,但开头必须是字母或下划线,且关键字不能作为标识符)
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;bool isvalid(char c){ if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')||c=='_') return true; else return false;}int main(){ char string[105]; int n; while(scanf("%d",&n)!=EOF) { char ch; scanf("%c",&ch); while(n--) { int flag=1; int i=0; scanf("%c",&ch); if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='_') flag=1; else flag=0; while(1) { scanf("%c",&ch); if(ch=='\n') { //cout<<"shabi"<<endl; break; } if(isvalid(ch)==0) flag=0; //cout<<flag<<" "<<"shabi"<<endl; } if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0;}
4、首字母变大写-hdu2026
题意:将英文句子的每个单词首字母变大写
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;int main(){ char String[105]; while(1) { memset(String,0,sizeof(String)); gets(String); int len=strlen(String); if(len==0) break; bool first=1; for(int i=0;String[i];i++) { if(first&&String[i]!=' ') printf("%c",String[i]+'A'-'a'); else printf("%c",String[i]); if(String[i]==' ') first=1; else first=0; } printf("\n"); } return 0;}
0 0
- 心得之gets
- gets
- gets()
- gets()
- gets
- gets()
- gets
- 执行计划之:consistent gets ,db block gets
- 字符串处理之gets与scanf("%s")的区别
- 字符与字符串处理之getchar,gets,putchar,puts函数
- Smarty之foreach心得
- OpenCL之我心得
- 人生之最心得
- 广义表之心得
- 研究心得之二
- 研究心得之三
- 十年 之 工作心得
- 读书心得之前言
- Android蓝牙源码分析——Gatt连接(一)
- UVA - 10856 Recover Factorial(二分查找)
- 经典算法学习——第一个只出现一次的字符
- CSS Hack
- 安装IIS6,不能复制文件的解决办法
- 心得之gets
- 题目
- Merit价值和成果管理系统——1侧栏与iframe
- 【30】Substring with Concatenation of All Words
- 解题报告:Codeforces Round #226 (Div. 2)E. Bear in the Field 矩阵加速幂
- Android中Socket使用
- 对Log日志的简易封装
- C++11标准之右值引用(ravalue reference)
- div span 区别