I have asked a question related to this program but now after much research and headbutting I am stuck...again.
I am trying to write a program that will take the user input and store it then print out all the unique words and the number of times they each occurred
for example
Please enter something: Hello#@Hello# hs,,,he,,whywhyto[then the user hits enter]
hello 2
hs 1
he 1
whywhyto 1
The above should be the output, of course whywhyto isn't a word but it doesn't matter in this case because I am assuming any pattern of letters separated by anything that isn't a letter (spaces, 0-9,#$(@ etc.) is considered a word. I need to use 2D arrays because I am not capable of using linked lists nor do I understand them yet.
This is all I have so far
#include <stdio.h>
#include <ctype.h>
int main()
{
char array[64];
int i=0, j, input;
printf("Please enter an input:");
input=fgetc(stdin);
while(input != '\n')
{
if(isalpha(input))
{
array[i]=input;
i++;
}
input=fgetc(stdin);
}
for(j=0;j<i;j++)
{
// printf("%c ",j,array[j]);
printf("%c",array[j]);
}
printf("\n");
}
I am using isalpha
to get only letters but all this does is it gets rid of anything that isn't a letter, stores it and then prints back, but I have not a clue on how to get it to store words once for their first occurrence and then just increment a count for each word. I can only use fgetc() which is hard for me at least, I only have about 3-4 months of C experience, I know I will have to use 2 dimensional arrays, have been reading up on them but I have not been able to comprehend how I will implement them please help me out a bit.
Here is code that seems to work:
Sample output:
The test for 'word too long' and 'too many words' help reassure me that the code is sound. Devising such tests is good practice.