|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#define _STDC_WANT_LIB_EXT1 1
#include <stdio.h>
#include<string.h>
#include<stddef.h>
#define TEXT_LEN 10000
#define BUF_SIZE 100
#define MAX_WORDS 500
#define WORD_LEN 12
#define ture 1
#define false 0
// strcat_s(char*strDestination,size_t numberOfElements,constchar *strSource) //申明strcat_s函数
int main()
{
char delimiters []="\n\".,;!?)(";
char text [TEXT_LEN]="";
char buf [BUF_SIZE];
char words[MAX_WORDS][WORD_LEN];
int nword[MAX_WORDS] ={0};
int word_count = 0;
int i=0;
//unsigned int size_t;
printf("Enter text on an arbittrary number of lines.");
printf("\nEnter an empty line to end inpupt:\n");
//Read an arbitarary numnber of lines of text
while (ture)
{
size_t len = TEXT_LEN;
char *ptr = NULL;
char *pWord = strtok_s(text,&len,delimiters,&ptr);
char new_word = ture;
//An empty string contingjust a newline
fgets(buf,BUF_SIZE,stdin);
if(buf[0] == '\n')
{
break;
}
//Check if we have space for latest input
if(strcat_s(text,sizeof(text),buf))
{
printf("Maximum capactity fott text exceted ,Terminating program.\n");
return 1;
}
//Find the frist word
// size_t len = TEXT_LEN;
// size_t len = TEXT_LEN;
// char *ptr = NULL;
// char* pWord = strtok_s(text, &len, delimiters,&ptr);
if(!pWord)
{
printf("No words found.Ending program.\n");
return 1;
}
strncpy_s(words[word_count],WORD_LEN,pWord);
++nword [word_count++];
while (ture)
{
int i = 0;
pWord = strtok_s(NULL,&len,delimiters,&ptr);
if(!pWord)
{
break;
}
for(i = 0;i<word_count;++i)
{
++nword[i];
new_word = false;
}
}
if(new_word)
{
strncpy_s(words[word_count],WORD_LEN,pWord);
++nword[word_count++];
}
else
new_word = ture;
if(word_count>MAX_WORDS -1)
{
printf("CAPACITY TO STORE WORDS EXCEEDED.\n");
return 1;
}
}
for(i = 0;i<word_count;i++)
{
printf("%-13s %3d",words[i],nword[i]);
if((i+1)%4==0)
printf("\n");
}
printf("\n");
return 0;
}
|
|