#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ret_words(char ***words) { //triple pointer argument
char str[50];
int count = 0;
char **temp = malloc(sizeof *temp); //allocate a pointer to to pointer to char
if(temp == NULL){ //allocation error
return -1;
}
do {
temp = realloc(temp, (count + 1) * sizeof *temp); //reallocate space at every scan
if(temp == NULL){
return -1;
}
temp[count] = malloc(strlen(str) + 1);
count++;
} while (scanf("%49s",temp[count - 1]) == 1 //scan str and append to temp
&& getchar() != '\n'); //with spaces between
*words = temp; //assing allocated array of pointers to argument
return count; //return word count
}
int main()
{
char **words; // pointer to pointer to navigate the array of strings
int ret;
puts("Enter the string");
putchar('>');
ret = ret_words(&words); //returned number of words on success, -1 on error
for(int i = 0; i < ret; i++) //print each stored one word string
printf("Word %d: %s\n",i + 1, words[i]);
printf("Word count: %d", ret); //print the number of words
}