okay, i'm wicked close now. It keeps correct count of all the words, outputs them correctly. But now it goes into an infinite loop after it prints out the output. I'm almost positive that the loop is coming from my print statement in main. here is my current code:
Code:
#include <stdafx.h>
#include <stdio.h>
#include <string.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]);
void countarray(char a[][MaxData], char b[][MaxData], int c[MaxData], int count);
int stringcompare(char a[MaxData], char b[MaxData]);
int main(void)
{
char inputarray[MaxData];
char newarray[MaxWords][MaxData];
char wordsarray[MaxWords][MaxData];
int numbersarray[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);
countarray(newarray, wordsarray, numbersarray, count);
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; wordsarray[counter1][counter2] != '\0'; counter2++)
{
printf("%c", wordsarray[counter1][counter2]);
}
printf("\t\t%d", numbersarray[counter1]);
}
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++;
}
}
}
int stringcompare(char a[MaxData], char b[MaxData])
{
for (int counter1 = 0 ; a[counter1] != '\0'; counter1++)
{
if (a[counter1] == b[counter1])
{
}
else
{
return 0;
}
}
return 1;
}
void countarray(char a[][MaxData], char b[][MaxData], int c[MaxData], int count)
{
int counter = 0;
int newwordcount = 0;
int charcounter = 0;
int testcounter = 0;
int same = 0;
int charcount1 = 0;
int isthesame = 0;
int numbers = 0;
char temparray1[MaxData] = "temp array 1";
char temparray2[MaxData] = "temp array 2";
for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++)
{
b[newwordcount][charcounter] = a[counter][charcounter];
}
b[newwordcount][charcounter] = '\0';
c[newwordcount] = 1;
newwordcount++;
for (counter = 1; counter < count; counter++)
{
same = 0;
for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++, charcount1++)
{
temparray1[charcounter] = a[counter][charcounter];
}
temparray1[charcounter] = '\0';
for (testcounter = 0; testcounter < newwordcount; testcounter++)
{
for (charcounter = 0; b[testcounter][charcounter] != '\0'; charcounter++, charcount1++)
{
temparray2[charcounter] = b[testcounter][charcounter];
}
temparray2[charcounter] = '\0';
isthesame = stringcompare(temparray1, temparray2);
if (isthesame == 0)
{
}
else
{
numbers = c[testcounter];
c[testcounter] = numbers + 1;
same = 1;
}
if (same == 1)
{
break;
}
}
if (same == 0)
{
for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++)
{
b[newwordcount][charcounter] = a[counter][charcounter];
}
b[newwordcount][charcounter] = '\0';
c[newwordcount] = 1;
newwordcount++;
}
}
}