避免数组或指针的下标越界,特别要当心发生“多 1”或者“少 1” 操作

避免数组或指针的下标越界,特别要当心发生“多 1”或者“少 1” 操作。

 

 1 #include <iostream>
 2 #include <string.h>
 3 
 4 
 5 using namespace std;
 6 
 7 char string[80];
 8 char seps[]   = " ,\t\n";
 9 char *token;
10 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
11 
12 int main(int argc, char** argv) {
13         //从键盘上输入两个语句
14     for (int i=1;i<3;i++) {
15         cout<<"Please input a sentence:"<<endl;
16         //整行输入
17         cin.getline(string,80);             
18         cout<<"Tokens:"<<endl;
19         //首次分离字符串
20         token = strtok( string, seps );        
21         while( token != NULL )              //结束分离判断
22         {
23             cout<<token<<endl;
24             //下次分离字符串
25             token = strtok( NULL, seps );   
26         }
27     }
28 }

 

posted @ 2018-08-03 13:39  borter  阅读(886)  评论(0编辑  收藏  举报