Question

In: Computer Science

Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a...

Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a C Language Program that will search for the following words and then print out the entire sentence from Lincoln’s Gettysburg Address for every occurrence of each of the specified words:

            civil                  nation              nobly               people             place               proper

            Your program should search through the Gettysburg Address and find the answers, not pre-load them to print out once a particular word is sought.

This is my current code that I came up with:

#include <stdio.h>
#include <string.h>

int main(void) {
   char address[] = "Fourscore and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate-we cannot consecrate-we cannot hallow-this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people shall not perish from the earth.";
   char line[50], word1 = "civil", word2 = "nation", word3 = "nobly", word4 = "people", word5 = "place", word6 = "proper";
   int loc, i, j, k=0;

   puts("Searching for the sentences that contain the words: civil, nation, nobly, people, place, proper\n");


   loc = strstr(address, word1); //searching for 'civil' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);

   loc = strstr(address, word2); //searching for 'nation' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);

   loc = strstr(address, word3); //searching for 'nobly' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);

   loc = strstr(address, word4); //searching for 'people' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);

   loc = strstr(address, word5); //searching for 'place' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);

   loc = strstr(address, word6); //searching for 'proper' in the string
   if (loc == NULL)
       puts("No match found.");
   else
       printf("%s", line);


   return 0;
}

What am I doing wrong here?

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>

int main(void) {
char address[] = "Fourscore and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate-we cannot consecrate-we cannot hallow-this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people shall not perish from the earth.";
char line[50], word1[] = "civil", word2[] = "nation", word3[] = "nobly", word4[] = "people", word5[] = "place", word6[] = "proper";
int i, j, k=0;
char* loc;
puts("Searching for the sentences that contain the words: civil, nation, nobly, people, place, proper\n");


loc = strstr(address, word1); //searching for 'civil' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);

loc = strstr(address, word2); //searching for 'nation' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);

loc = strstr(address, word3); //searching for 'nobly' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);

loc = strstr(address, word4); //searching for 'people' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);

loc = strstr(address, word5); //searching for 'place' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);

loc = strstr(address, word6); //searching for 'proper' in the string
if (loc == NULL)
puts("No match found.");
else
printf("%s\n", line);


return 0;
}


Related Solutions

How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
Recall that you took samples of words from the population of words in the Gettysburg Address,...
Recall that you took samples of words from the population of words in the Gettysburg Address, for which the average length of a word is 4.295 letters. 1.Is the number 4.295 a parameter or a statistic? 2.When you first selected your sample of 10 (circled) words, what was the variable that you recorded about the words? Was the variable categorical or quantitative? 3.You calculated the average (mean) number of letters per word in your sample of ten circled words. Is...
Article from that website address (copy the address and open it for read), and finish those...
Article from that website address (copy the address and open it for read), and finish those questions after read article. Thanks The Hidden Cost of Vanilla Use the article at https://www.danwatch.dk/en/undersogelse/thehiddencostofvanilla/ to answer the following questions. 1. According to the United Nations Development Programme (UNDP) what is the standard of living in Madagascar (what does the majority of the population live on per day?) 2. Who are the parties involved in producing and distributing vanilla from Madagascar? 3. Who has...
Article from that website address (copy the address and open it for read), and finish those...
Article from that website address (copy the address and open it for read), and finish those questions after read article. Thanks The Hidden Cost of Vanilla Use the article at https://www.danwatch.dk/en/undersogelse/thehiddencostofvanilla/ to answer the following questions. 1. According to the United Nations Development Programme (UNDP) what is the standard of living in Madagascar (what does the majority of the population live on per day?) 2. Who are the parties involved in producing and distributing vanilla from Madagascar? 3. Who has...
Article from that website address (copy the address and open it for read), and finish those...
Article from that website address (copy the address and open it for read), and finish those questions after read article. Thanks The Hidden Cost of Vanilla Use the article at https://www.danwatch.dk/en/undersogelse/thehiddencostofvanilla/ to answer the following questions. 1. According to the United Nations Development Programme (UNDP) what is the standard of living in Madagascar (what does the majority of the population live on per day?) 2. Who are the parties involved in producing and distributing vanilla from Madagascar? 3. Who has...
Please don’t copy from the same site and No copy from any source. The work should...
Please don’t copy from the same site and No copy from any source. The work should be your own words and answer. And please don’t answers be handwriting Discuss an example of an expert system in any filed and explain the importance of creating this system.
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT