Here are some things you can add to your solution:
- Check
fgets()
for return value, as it returns NULL
on error.
- If you decide to still use
scanf()
, make sure to use scanf("%255s", input)
instead for char input[256]
. Using the format specifier %255s
instead of the simpe %s
checks for excessive input. Overall, it just better to read input using fgets()
.
- Remove
'\n'
character appended by fgets()
. This is also good for checking that you don't enter more characters than the limit of 256
in input
, and that your sentences don't have a trailing newline after each of them. If you don't remove this newline, then your strtok()
delimiter would have to be " \n"
instead.
#define
constants in your code, and use const char*
for string literals, such as the delimiter for strtok()
.
- You can also add some code to check for empty inputs from
fgets()
. You could simply use a separate counter, and only increment this counter for valid strings found.
- It's also strange to have
struct
with one member, usually structs contain more than one member. You could simply bypass using a struct and use a 2D char array declared as char listexcuses[NUMEXCUSES][EXCUSELENGTH]
. This array can hold up to 20
strings, each of which has a maximum length of 256
.
Here is some modified code of your approach:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EXCUSELENGTH 256
#define NUMEXCUSES 20
typedef struct {
char sentence[EXCUSELENGTH];
} excuse;
int main(void) {
excuse listexcuses[NUMEXCUSES];
char input[EXCUSELENGTH] = {'\0'};
char *word = NULL;
const char *delim = " ";
size_t slen, count = 0;
for (size_t i = 0; i < NUMEXCUSES; i++) {
printf("\nEnter excuse number %zu:\n", count+1);
if (fgets(input, EXCUSELENGTH, stdin) == NULL) {
fprintf(stderr, "Error from fgets(), cannot read line\n");
exit(EXIT_FAILURE);
}
slen = strlen(input);
if (slen > 0 && input[slen-1] == '\n') {
input[slen-1] = '\0';
} else {
fprintf(stderr, "Too many characters entered in excuse %zu\n", count+1);
exit(EXIT_FAILURE);
}
if (*input) {
strcpy(listexcuses[count].sentence, input);
count++;
printf("\nTokens found:\n");
word = strtok(input, delim);
while (word != NULL) {
printf("%s\n", word);
word = strtok(NULL, delim);
}
}
}
return 0;
}
As you need to eventually store these tokens somewhere, you will need another form of storing this data. Since you don't know how many tokens you can get, or how long each token is, you may need to use something like char **tokens
. This is not an array, but it is a pointer to a pointer. Using this would allow any number of words and any lengths of each word to be stored. You will need dynamic memory allocation for this. The answer in this post will help.