text processing assignment
The goal of this assignment is to count the number of words in a user entered string, count the number of letters in the shortest and longest word, the average number of letters used, the number of newline characters used, and the number of times each word appears in the text. I have the first 5 parts done and cannot get the last part to work (the part that counts how many times each word appears).
my professor gave us the following function prototype and description:
void addword(char wordarray[][MAXWORD], char *word, int wordcount[], int *numwords);
if the word is present in the word array increment the words count. if it isn't add the word to the array and set the appropriate element in word count to 1.
Parameters:
wordarray - array of words in text
word - word to be processed
wordcount - array of wordcount
numwords - counter for total number of words in the array
I have no idea how to begin to utilize this seeing as i only have a one dimensional input array.
This is my code (I included the whole program because it basically encompasses everything I've learned so far, just to give you all an idea of my programming abilities):
Code:
#include <stdafx.h>
#include <stdio.h>
#define MaxData 1000
#define MaxWords 100
int wordcount(char a[MaxData]);
int shortestword(char a[MaxData]);
int longestword(char a[MaxData]);
int averagesize(char a[MaxData], int wordcount);
int newlinecount(char a[MaxData]);
void createnewarray(char a[], char b[MaxWords][MaxData]);
int main(void)
{
char inputarray[MaxData];
char newarray[MaxWords][MaxData];
int counter1 = 0;
int counter2 = 0;
int count = 0;
int shortest = 0;
int longest = 0;
int average = 0;
int newline = 0;
printf("Input the text to be tested: \n");
scanf("%[^\0]", inputarray);
count = wordcount(inputarray);
shortest = shortestword(inputarray);
longest = longestword(inputarray);
average = averagesize(inputarray, count);
newline = newlinecount(inputarray);
createnewarray(inputarray, newarray);
printf("\nThere are %d words in the text.\n", count);
printf("The shortest word has %d letters in it.\n", shortest);
printf("The longest word has %d letters in it.\n", longest);
printf("The average size of a word is %d letters.\n", average);
printf("There were %d newline characters used.\n", newline);
for (counter1 = 0; counter1 != count; counter1++)
{
printf("\n");
for(counter2 = 0; newarray[counter1][counter2] != '\0'; counter2++)
{
printf("%c", newarray[counter1][counter2]);
}
}
for(;;);
return 0;
}
int wordcount(char a[MaxData])
{
int wordcounter = 0;
char *counter;
for (counter = &a[0]; *counter != '\0'; counter++)
{
if ((*counter == ' ') || (*counter == '\n'))
{
wordcounter++;
}
}
return wordcounter;
}
int shortestword(char a[MaxData])
{
int charcounter = 0;
int shortestword = 500;
char *counter;
for (counter = &a[0]; *counter != '\0'; counter++)
{
charcounter++;
if((*counter == ' ') || (*counter == '\n'))
{
charcounter--;
if(charcounter < shortestword)
{
shortestword = charcounter;
charcounter = 0;
}
else
charcounter = 0;
}
}
return shortestword;
}
int longestword(char a[MaxData])
{
int charcounter = 0;
int longestword = 0;
char *counter;
for (counter = &a[0]; *counter != '\0'; counter++)
{
charcounter++;
if((*counter == ' ') || (*counter == '\n'))
{
charcounter--;
if(charcounter > longestword)
{
longestword = charcounter;
charcounter = 0;
}
else
charcounter = 0;
}
}
return longestword;
}
int averagesize(char a[MaxData], int wordcount)
{
int charcounter = 0;
int averageword = 0;
char *counter;
for (counter = &a[0]; *counter != '\0'; counter++)
{
charcounter++;
if((*counter == ' ') || (*counter == '\n'))
{
charcounter--;
averageword += charcounter;
charcounter = 0;
}
}
averageword = averageword / wordcount;
return averageword;
}
int newlinecount(char a[MaxData])
{
int newlinecounter = 0;
char *counter;
for (counter = &a[0]; *counter != '\0'; counter++)
{
if(*counter == '\n')
{
newlinecounter++;
}
}
return newlinecounter;
}
void createnewarray(char a[], char b[MaxWords][MaxData])
{
char *counter;
int bcounter = 0;
int words = 0;
for (counter = &a[0]; *counter != '\0'; counter++)
{
if ((*counter == ' ') || (*counter == '\n'))
{
b[words][bcounter] = '\0';
words++;
bcounter = 0;
}
else
{
b[words][bcounter] = *counter;
bcounter++;
}
}
}