I have this code in my book, but I tried to understand it for a long time, but I couldn't. So, the code takes input from the user, and prints the outputs word per line along with the size of the word. It also prints the output based on the line. And it exclude any . , ! etc.. Example:
Input:
Hello, I am new here.
trying to learn.
Output:
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)
Also, The code works on my friends computer, but not on mine. I keep getting an error message as you can see below. Can you please explain it to me? Also, why it doesn't work with me? and how can I fix it? I tried to use fgets, but it didn't work.
#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;
}