- Joined
- Aug 8, 2005
- Location
- Ca, Los Angles
What im trying to do is use the input paragraph(at the bottom) through redirection in a command line and put each word into a structure array.What i think is wrong is how my input function passes a word to the record function - logical and syntax wise
also i will have to alphabetize the array of structures.
also i will have to alphabetize the array of structures.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Word
{
char word[10];
int lineNumber;
int count;
}WORD;
void input(WORD *);
void sort(WORD *);
void output();
int main(void)
{
WORD *structArray = (WORD *) malloc( 100*sizeof(WORD) );
memset( structArray, 0, sizeof(structArray) );
input(structArray);
sort(structArray);
output(structArray);
system("pause");
return 0;
}
void input(WORD *structArrayPointer)
{
char ch;
int lineCounter = 1, i = 0, number = 0;
char *bufferPtr;
bufferPtr = (char *) malloc( 10 * sizeof(char) );
bufferPtr[9] = '\0';
while ( ( ch = getchar() ) != EOF )
{
if ( ch != ' ' && ch != '.' && ch != ',' || ch != '\n')
{
bufferPtr[i] = ch;
}
else
{
i = 0; //reset the buffer for next word
if (ch == '\n') lineCounter++;
strcpy(structArrayPointer[number].word, bufferPtr);
structArrayPointer[number].lineNumber = lineCounter;
number++; //keeps the entries in order
}
i++;
}
}
void output(WORD *structArrayPointer)
{
int i;
for(i = 0; i < 100; i++)
{
printf("word #%d is %s\n", i + 1, structArrayPointer[i].word );
printf("Its on line number %d\n", structArrayPointer[i].lineNumber );
printf("It appears %d time(s)\n", structArrayPointer[i].count );
}
}
void sort(WORD *structArrayPtr)
{
int i, temp;
WORD tempStruct;
for (i = 0; i < 100, i++)
{
temp = strcmp( structArrayPtr[i].word, structArrayPtr[i+1].word);
if (temp == 0){} //same
if (temp > 0) //s1>s2
{
tempStruct = structArrayPtr[i];
structArrayPtr[i] = structArrayPtr[i+1];
structArrayPtr[i+1] = tempStruct;
}
}
}
/* Input Paragraph
The following are the laws of computer programming. Any given program, when running, is obsolete. Any
given program costs more and takes longer. If a program is useful, it will have to be changed. If a program
is useless, it will have to be documented. Any given program will expand to fill all the available memory.
the value of a program is proportional to the weight of its output. Program complexity grows until it
exceeds the capability of the programmer who must maintain it. Make it possible for programmers to write
programs in English, and you will find that programmers cannot write in English.
*/
Last edited: