我的书中有这段代码,但是很长一段时间我都试图理解它,但我没有。因此,该代码从用户处获取输入,并每行打印输出的单词以及单词的大小。它还根据该行打印输出。并且不包括任何内容。 ,!等等.. 示例:
输入:
Hello,I am new here.
trying to learn.
输出:
Words typed on line number 1:
Hello(5)
I(1)
am(2)
new(3)
here(4)
Words typed on line number 2:
trying(6)
to(2)
learn(5)
此外,该代码可在我的朋友计算机上使用,但不能在我的计算机上使用。我不断收到错误消息,如下所示。你能给我解释一下吗?另外,为什么它对我不起作用?以及如何解决?我尝试使用fgets,但是没有用。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
char str[10][81];
char* ptoken,* ptr;
int i = 0,maxstr = 0;
char* exclude = " .,!\t"; // I get an error message here (This entity cannot be initialized.)
printf("Enter the text you want on multiple lines\n");
printf("after you enter every thing pres ctrl+z and Enter \n\n");
while (gets(str[i])) // I get an error message here (The identifier gets cant be found).
{
str[i++];
maxstr++;
}
for (i = 0; i < maxstr; i++) {
printf("\n<< Words typed on line number %d> \n",i + 1);
ptr = str[i];
ptoken = strtok(ptr,exclude);
while (ptoken != NULL) {
printf("strlen(%s) = %d\n",ptoken,strlen(ptoken));
ptoken = strtok(NULL,exclude);
}
}
return 0;
}