新手关于自己写的一段小练习
新手求助关于自己写的一段小练习
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define TEXTLEN 10000 /*存储文本的最大长度*/
#define BUFFERSIZE 100 /*每次读入输入的最大长度*/
#define MAXWORDS 500 /*存储的最大单词数*/
#define WORDLEN 15 /*单词的最大长度*/
int main(void)
{
char text[TEXTLEN]; /*声明存储输入的文本*/
char buffer[BUFFERSIZE]; /*声明存储输入的临时地方*/
char endstr[]="*\n"; /*用户结束输入的标志*/
const char space=' '; /*空格*/
char words[MAXWORDS][WORDLEN]; /*所有单词的存储地址*/
char word[WORDLEN]; /*单个单词存储地址*/
int wordlen=0; /*判断单词长度*/
int i=0; /*挑出单词循环计数器*/
int words_number=0; /*所有存储单词的索引*/
printf("请输入单词,单词间用逗号分开(输入完毕单起一行以*号键结束):\n");
/*单词输入的程序*/
while(true)
{
if(!strcmp(fgets(buffer,BUFFERSIZE,stdin),endstr))
break;
if(strlen(text)+strlen(buffer)+1>TEXTLEN)
{
printf("超出输入的最大限度,结束程序!");
return 1;
}
strcat(text,buffer);
}
/*将文本中除空格和字母以外的一切字符替换成空格*/
for(i=0;i<strlen(text);i++)
{
if(text[i]==space||isalpha(text[i]))
continue;
text[i]=space;
}
/*在文本之中找单词并将其存储*/
i=0;
while(true)
{
/*跳过空格*/
while(text[i]==space)
i++;
/*检查是否到达文本结尾*/
if(text[i]='\0')
break;
/*挑出单词*/
wordlen=0;
while(isalpha(text[i]))
{
/*检查单词是否过长*/
if(wordlen==WORDLEN)
{
printf("单词过长,程序结束!");
return 1;
}
word[wordlen++]=text[i++];
}
word[wordlen]='\0';
/*将单挑出的单词保存*/
if(words_number>=MAXWORDS)
{
printf("\n超出单词数目范围,程序结束!");
return 1;
}
strcpy(words[words_number],word);
words_number++;
}
/*将所有单词输出*/
printf("\n你输入的单词是:\n");
for(i=0;i<words_number;i++)
printf("%s\n",words[i]);
system("Pause");
return 0;
}
那位高人帮忙看看到底哪里有问题啊?代码可以编译,但一运行,等我输入了单词以后立刻退出,在下看了好几遍还是没找到问题所在,求指点
------解决方案--------------------
if(text[i]='\0') // =号
break;
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define TEXTLEN 10000 /*存储文本的最大长度*/
#define BUFFERSIZE 100 /*每次读入输入的最大长度*/
#define MAXWORDS 500 /*存储的最大单词数*/
#define WORDLEN 15 /*单词的最大长度*/
int main(void)
{
char text[TEXTLEN]; /*声明存储输入的文本*/
char buffer[BUFFERSIZE]; /*声明存储输入的临时地方*/
char endstr[]="*\n"; /*用户结束输入的标志*/
const char space=' '; /*空格*/
char words[MAXWORDS][WORDLEN]; /*所有单词的存储地址*/
char word[WORDLEN]; /*单个单词存储地址*/
int wordlen=0; /*判断单词长度*/
int i=0; /*挑出单词循环计数器*/
int words_number=0; /*所有存储单词的索引*/
printf("请输入单词,单词间用逗号分开(输入完毕单起一行以*号键结束):\n");
/*单词输入的程序*/
while(true)
{
if(!strcmp(fgets(buffer,BUFFERSIZE,stdin),endstr))
break;
if(strlen(text)+strlen(buffer)+1>TEXTLEN)
{
printf("超出输入的最大限度,结束程序!");
return 1;
}
strcat(text,buffer);
}
/*将文本中除空格和字母以外的一切字符替换成空格*/
for(i=0;i<strlen(text);i++)
{
if(text[i]==space||isalpha(text[i]))
continue;
text[i]=space;
}
/*在文本之中找单词并将其存储*/
i=0;
while(true)
{
/*跳过空格*/
while(text[i]==space)
i++;
/*检查是否到达文本结尾*/
if(text[i]='\0')
break;
/*挑出单词*/
wordlen=0;
while(isalpha(text[i]))
{
/*检查单词是否过长*/
if(wordlen==WORDLEN)
{
printf("单词过长,程序结束!");
return 1;
}
word[wordlen++]=text[i++];
}
word[wordlen]='\0';
/*将单挑出的单词保存*/
if(words_number>=MAXWORDS)
{
printf("\n超出单词数目范围,程序结束!");
return 1;
}
strcpy(words[words_number],word);
words_number++;
}
/*将所有单词输出*/
printf("\n你输入的单词是:\n");
for(i=0;i<words_number;i++)
printf("%s\n",words[i]);
system("Pause");
return 0;
}
那位高人帮忙看看到底哪里有问题啊?代码可以编译,但一运行,等我输入了单词以后立刻退出,在下看了好几遍还是没找到问题所在,求指点
------解决方案--------------------
if(text[i]='\0') // =号
break;