In: Computer Science
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?
#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;
}