Need it in 5-6 hrs from now..C programming..
CSE 220 Assignment 3 Hints and Tips Some hints for approaching this assignment is as follows: Approaching the assignment : General Overview It would be a good thing if before you start your assignment you try to decompose the problem you are trying to solve into small segments. Each segment can be then written and tested for any bugs or errors. Doing so will help you learn the true nature of functions and how effective they can be. The assignment requires that you read a string from either the stdin or a file and then count the frequency of words in it and display it to stdout or to a file. We can look at this in the following way :
1) Read an input from one of two sources a. stdin b. an input file
2) Find the frequency of words from input string 3) Write the words and their frequency to one of two ouputs
a. Stdout b. An outputifle
One way of solving the problem One way of approaching this code would be to create a structure which will hold a word and its frequency and then create an array of that structure to accommodate all the unique words in the input. So you would create a structure as follows: struct wordStorage { char word[50]; int count; }; Then when you want to create an array of that structure you would write it out as : struct wordStorage arr[size]; So we created an array to of size number of elements of the structure wordStorage created above. This array can then be used to keep track of all the unique words you come across in the input string and their frequencies. You can create arrays of this structure after you have defined the structure itself.
You could then use the following prototypes in your code:
1. void decomposeToArray(char *para,char wordarr[1000][50]) This function takes in a string para and stores all the words in that string. You can modify it to return the array of words containing all the words using – char** decomposeToArray(char *para,char wordarr[1000][50]);
2. void frequencyOfWords(struct wordStorage *wordarr,char wordList[1000][50])
This function takes in a structure array in which element contains a word and its frequency and the array of words that you created using decomposeToArray(args). Instead of using a void function you can modify it so that it stores the words and their frequency as an array of the given structure and then returns the structure array. The prototype then becomes – struct wordStorage* frequencyOfWords(wordList[1000][50])
3. void readFromFile(char *filename,char sentence[1000])
This function takes in the name of a file and a string to store all the contents of the file. You can use pass by reference to keep track of the string that is storing the string OR you could modify the function to read the name of a file and return a string using: char* readFromFile(char *filename)
4. void writeToFile(char *filename,struct wordStorage *wordarr);
This function prints the contents of the structure array wordarr into the file whose name is specified by filename
5. void displayWords(struct wordStorage *wordarr); This functions writes the contents of the structure array ( containing words and their frequencies) on the standard output i.e. your display
The prototypes mentioned above will then need to be linked in the main function depending on the arguments that are passed through the command line. For example : You can read a string from stdin and use decomposeToArray(args) to break your input string into a list of words. This list can be stored as a 2D character array and then sent to the function frequencyOfWords(list_you_got_from_decomposeToArray) and the function will return a structure array that consists of all the unique words in the string and their frequencies. [Note : frequencyOfWords() must have a structure array inside it in order to be able to return it. If you are using the void frequencyOfWords(struct wordStorage *wordarr,char wordList[1000][50]) variation, then you will need to use Pass By Reference to handle it.]
Dealing with 2D Character arrays in C: 2D arrays in C can be a little overwhelming to understand especially when dealing with strings. It is important to understand the structure of the 2D character array while dealing with an array of strings. For example if you were to store a string in a single dimensional character array you could do it in the following ways : char *array=”This is a string in C!”; char array[]=”This is a string in C!”; char array[100]; strcpy(array,”This will also be a string!”); So you can see that a string in C is basically an array of characters. When dealing with an array of strings, you need to look at it like an array of strings or in our case an array of arrays ( where each string is an array!) So if we were to accept a sentence from the user and then store each word in an array, we would need a 2D array to do so. Think of it like this : char words[rows][cols]; Each row is a word and each column in a row is a character belonging to a certain word. So when you find a character in a word, you add it to the column of the present row ( the present word you are looking at). If a word is completed, then you go to the next row. The way to approach this would be to first accept the sentence from the user. Then try to decompose that sentence by selecting end of word markers ( or string delimiters). In the example which follows below, we assume that the space (‘ ‘) character is what signifies the end of a word. So if we had a string like : “This is a new sentence “, then we should be able to break it into 5 words where each word ends with a space character(‘ ‘). Now once we have set our word delimiters we need to check each character in the input string and decide whether a word is completely read or not. In order to do so, run a loop, extract each character into a dummy variable and check if it is your word delimiter ( here we use space only, you should consider punctuation marks and so on). If a character is not the word delimiter, add it to the word count 2D array along the second dimension( columns in the 2D array) .Otherwise if it is a word
delimiter, then a word has been completely read from the input string – so we increment the counter on the 1st dimension ( rows of the 2D array used) of the 2D array to go to the next word slot. For example : char words[100][100]; // Assume that we are going to have a maximum of 100 words of size 100 characters each char sentence[1000]; //Assuming that the input string is just composed of 1000 characters char ch; // A dummy variable to act as a buffer for the input string int len,wc=0,c=0,i=0; printf("Enter a sentence:"); //Using the %[\n]s we can force the scanf to ignore \n characters. This allows us to input \n scanf("%[\n]s",&sentence); strcat(sentence," "); // I am assuming that every word in the string ends with a space len=strlen(sentence); //Calculate the length of the string input //The next part will show you how each word is read from the string and then stored inside a // 2D array for processing later for(i=0;i<len;i++) { ch=sentence[i]; //Read each character from the input string if(ch==' ') // If you have reached a space (‘ ‘)character , it implies a word is completely read { words[wc][c++]='\0'; // Terminate each string with a ‘\0’ character wc++; // We just finished adding a new word c=0; //Reset the counter which keeps track of the letter inside the word } else // If you read anything other than a ‘ ‘, add it as a letter to the present word { words[wc][c++]=ch; } }