#include <stdio.h>
#include <string.h>
const int MAX=256;
//应用指针统计一个字符串中有多少个单词
int main()
{
char* nums[]={ "one" , "two" , "tree" , "four" , "five"\
, "six" , "seven" , "eight" , "nine" , "ten" };
char word[MAX]={'\0'};
char* test="This is a test number of words in the sentences.";
char* p1=test;
char* p2=test;
int len=0;
int index=0;
while(1)
{
if (*(p2+1)!=0x20)
{
p2++;
if (*p2!='\0') continue;
}
p2++;
len=(int)(p2-p1);
strncpy(word,p1,len);
word[len]='\0';
printf("the %5s word is : %s\n",nums[index],word);
p2++;
p1=p2;
index++;
if (*p2=='\0') break;
}
return 0;
}
/*
样例输出:
the one word is : This
the two word is : is
the tree word is : a
the four word is : test
the five word is : number
the six word is : of
the seven word is : words
the eight word is : in
the nine word is : the
the ten word is : sentences.
*/