编写代码实现从输入流中分析出数字串,输入串为:
A012BCD378 274D
EF55G^Z
输出为:
Digit string 1 is: 012
Digit string 2 is: 378
Digit string 3 is: 274
Digit string 4 is: 55
使用到的相关函数:
getline()
语法:
istream &getline( char *buffer, streamsize num );
istream &getline( char *buffer, streamsize num, char delim );
功能:函数用于输入流,读取字符到buffer中,直到下列情况发生:
num - 1个字符已经读入,
碰到一个换行标志,
碰到一个EOF,
或者,任意地读入,直到读到字符delim。delim字符不会被放入buffer中。
strtok()
语法:
#include <string.h>
char *strtok( char *str1, const char *str2 );
功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
源码:
- #include<iostream.h>
- #include <string.h>
- void main()
- {
- char str[200];
- cin.getline(str,200);
- int num=1;
- char*p=strtok(str,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ");
- while(1)
- {
- if(!p)break;
- if(strlen(p))
- {
- cout<<"Digit string "<<num<<" is: "<<p<<endl;
- num++;
- }
- p=strtok(0,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ");
- }
- }
如果你想与我交流,请点击如下链接加我为好友:http://student.csdn.net/invite.php?u=111207&c=20fb91d9e4f65a83