Ok I have written so code that is in a text format from a file. It is supposed to separate the words and then eliminate "the", "and", "an", "for", "a", and "of".
I'm trying to KISS. But I'm hung up on allocating the string of characters to memory. I can get the string of charcters to print in but am have troubles getting it to run past my calloc memory allocation. Can you please help. I am not sure where I'm going wrong. This is my code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FLUSH while (getchar() != '\n')
int main (void)
{
// Local Declarations
char strng[1000];
char** pWords; // array of pointers to char
FILE* spIn;
int size = 100;
int wordIndex;
// Statements
printf("Data to Manipulate: \n\n");
if (!(spIn = fopen ("c:\\sample.txt", "r")))
{
printf("\aError could not open.\n");
system("Pause");
exit (100);
}//if
while (fgets (strng, sizeof (strng), spIn))
{
printf("%s", strng);
}
printf("\n\n");
// Allocate array in heap.
// One extra element added for loop control
pWords = (char*)calloc (size + 1, sizeof *(char*));
wordIndex = 0;
while (wordIndex < size
&& fgets(strng, sizeof(strng), spIn))
{
*(pWords + wordIndex) = (char*)
calloc (strlen(strng) + 1, sizeof(char));
strcpy (*(pWords + wordIndex), strng);
wordIndex++;
} // while
*(pWords + wordIndex) = NULL;
printf("\nList of words are: \n");
wordIndex = 0;
while (*(pWords + wordIndex))
{
printf("%3d: %s",
wordIndex, *(pWords + wordIndex));
wordIndex++;
} // while
system("Pause");
return 0;
} // main
And here is the File used for in.
****
Creating a national seal was a more difficult task then our founding fathers
believed it would be. The idea was to create a national seal that would
illustrate the principles and ideals of our new nation. The challenge was
agreeing upon the symbols that would best represent the thirteen states as a
whole, the government including Congress, the Senate, and the Commander and
Chief while representing the ideals and principles fought for and resulted in
blood shed. The process of designing the seal was one of heated debate and
disagreement. The design of the seal ended up being a six-year project
resulting in several proposed designs from three separate committees.
****
Thank you in advance. I can'yt move on until I figure this out and I'm at my wits end. PLease Please help.
Thank you!