Question

In: Computer Science

Assignment in C programming class: read the text file line by line, and place each word,...

Assignment in C programming class:
read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a word. However, there is no guarantee that the text will have 1000 words or less and any word in the text is 10 characters or less

Solutions

Expert Solution

/*
Assignment in C programming class:
read the text file line by line, and place each word, in order, in an array of char strings. 
As you copy each word into the array, you will keep a running count of the number of words 
in the text file and convert the letters of each word to lower case. 
Assume that you will use no more than 1000 words in the text, and no more than the first 
10 characters in a word. However, there is no guarantee that the text will have 1000 words 
or less and any word in the text is 10 characters or less
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// usage : textfile is to be read as command line argument
// eg: ./a.out myFile
int main(int argc, char * argv[]) 
{ 
        if (argc < 2) return 1; 
        char * filename = argv[1]; 
        FILE * fp = fopen(filename, "r"); 
        if (fp == NULL) return 1; 
        char word[10];
        int countWords = 0;
        char allWords[1000][10];
    /* assumes no word exceeds length of 1023 */
    while (fscanf(fp, " %1023s", word) > 0) {
        for(int i = 0; word[i] != '\0'; i++)
                word[i] = tolower(word[i]);
        strcpy(allWords[countWords], word);
        countWords++;
        // puts(allWords[countWords-1]);
    }
        return 0; 
}

Related Solutions

Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
C Programming file.c takes in one input argument that denotes a file to be read. It...
C Programming file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *). Please fix and or complete the program, and explain any of the changes too: ---- #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *f; f = fopen(argv[1], "r"); if( !f ) { exit(1); }...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT