IN C++ Given a paragraph of text sort and print (to a file) all of the words in
ID: 3735851 • Letter: I
Question
IN C++
Given a paragraph of text sort and print (to a file) all of the words in alphabetical order. For this program you will need to first input all the words and store them in a string array. Then sort the words using either the bubble sort or selection sort. After they are sorted write them out to a new file. The output file should contain 10 words per line. Use this file as input.
INPUT FILE DATA
Four score 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 dedicated, can long endure. We are met on a great battle-field 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 can not dedicate -- we can not consecrate --
we can not 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.
NOTE:
The words vary in length. This does not matter.
Uppercase letters have a lower ASCII code than lowercase letters. Words beginning with a capital letter will thus appear before words beginning with lowercase letters following the sort.
There are some non-word strings in the text - like "--". Just allow those strings to be sorted as if they were a word.
You do not need the dictionary for this problem.
Upload the text file along with your code
Explanation / Answer
Program
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(string list[], int length);
int main()
{
// filestream variable file
fstream file;
string word, filename;
string words [300];
int count=0,i;
// filename of the file
filename = "input.txt";
// opening file
file.open(filename.c_str());
// extracting words form the file
while (file >> word)
{
// displaying content
cout << word << endl;
words[count]=word;
count++;
}
file.close();
bubbleSort(words,count);
return 0;
}
void bubbleSort(string list[300], int length)
{
ofstream fp;
string temp,word;
int iteration;
int index,i,j;
fp.open("output.txt");
for (iteration = 1; iteration < length; iteration++)
{
for (index = 0; index < length - iteration; index++)
if (list[index] > list[index + 1])
{
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
for(i=0;i<length;i++)
{
j=0;
while(j<10)
{
fp<<list[i];
j++;
}
fp<<" ";
}
fp.close();
}
Output File
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
But,But,But,But,But,But,But,But,But,But,
FourFourFourFourFourFourFourFourFourFour
God,God,God,God,God,God,God,God,God,God,
ItItItItItItItItItIt
ItItItItItItItItItIt
ItItItItItItItItItIt
Liberty,Liberty,Liberty,Liberty,Liberty,Liberty,Liberty,Liberty,Liberty,Liberty,
NowNowNowNowNowNowNowNowNowNow
TheTheTheTheTheTheTheTheTheThe
TheTheTheTheTheTheTheTheTheThe
WeWeWeWeWeWeWeWeWeWe
WeWeWeWeWeWeWeWeWeWe
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aboveaboveaboveaboveaboveaboveaboveaboveaboveabove
addaddaddaddaddaddaddaddaddadd
advanced.advanced.advanced.advanced.advanced.advanced.advanced.advanced.advanced.advanced.
agoagoagoagoagoagoagoagoagoago
allallallallallallallallallall
altogetheraltogetheraltogetheraltogetheraltogetheraltogetheraltogetheraltogetheraltogetheraltogether
andandandandandandandandandand
andandandandandandandandandand
andandandandandandandandandand
andandandandandandandandandand
andandandandandandandandandand
andandandandandandandandandand
anyanyanyanyanyanyanyanyanyany
areareareareareareareareareare
areareareareareareareareareare
areareareareareareareareareare
asasasasasasasasasas
battle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-fieldbattle-field
bebebebebebebebebebe
bebebebebebebebebebe
beforebeforebeforebeforebeforebeforebeforebeforebeforebefore
birthbirthbirthbirthbirthbirthbirthbirthbirthbirth
bravebravebravebravebravebravebravebravebravebrave
broughtbroughtbroughtbroughtbroughtbroughtbroughtbroughtbroughtbrought
butbutbutbutbutbutbutbutbutbut
bybybybybybybybybyby
cancancancancancancancancancan
cancancancancancancancancancan
cancancancancancancancancancan
cancancancancancancancancancan
cancancancancancancancancancan
causecausecausecausecausecausecausecausecausecause
civilcivilcivilcivilcivilcivilcivilcivilcivilcivil
comecomecomecomecomecomecomecomecomecome
conceivedconceivedconceivedconceivedconceivedconceivedconceivedconceivedconceivedconceived
conceivedconceivedconceivedconceivedconceivedconceivedconceivedconceivedconceivedconceived
consecrateconsecrateconsecrateconsecrateconsecrateconsecrateconsecrateconsecrateconsecrateconsecrate
consecratedconsecratedconsecratedconsecratedconsecratedconsecratedconsecratedconsecratedconsecratedconsecrated
continent,continent,continent,continent,continent,continent,continent,continent,continent,continent,
createdcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreatedcreated
deaddeaddeaddeaddeaddeaddeaddeaddeaddead
deaddeaddeaddeaddeaddeaddeaddeaddeaddead
dead,dead,dead,dead,dead,dead,dead,dead,dead,dead,
dedicatededicatededicatededicatededicatededicatededicatededicatededicatededicate
dedicatededicatededicatededicatededicatededicatededicatededicatededicatededicate
dedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicated
dedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicated
dedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicateddedicated
dedicated,dedicated,dedicated,dedicated,dedicated,dedicated,dedicated,dedicated,dedicated,dedicated,
detract.detract.detract.detract.detract.detract.detract.detract.detract.detract.
devotiondevotiondevotiondevotiondevotiondevotiondevotiondevotiondevotiondevotion
devotiondevotiondevotiondevotiondevotiondevotiondevotiondevotiondevotiondevotion
diddiddiddiddiddiddiddiddiddid
dieddieddieddieddieddieddieddieddieddied
dodododododododododo
earth.earth.earth.earth.earth.earth.earth.earth.earth.earth.
endure.endure.endure.endure.endure.endure.endure.endure.endure.endure.
engagedengagedengagedengagedengagedengagedengagedengagedengagedengaged
equal.equal.equal.equal.equal.equal.equal.equal.equal.equal.
farfarfarfarfarfarfarfarfarfar
farfarfarfarfarfarfarfarfarfar
fathersfathersfathersfathersfathersfathersfathersfathersfathersfathers
field,field,field,field,field,field,field,field,field,field,
finalfinalfinalfinalfinalfinalfinalfinalfinalfinal
fittingfittingfittingfittingfittingfittingfittingfittingfittingfitting
forforforforforforforforforfor
forforforforforforforforforfor
forforforforforforforforforfor
forforforforforforforforforfor
forforforforforforforforforfor
forgetforgetforgetforgetforgetforgetforgetforgetforgetforget
forthforthforthforthforthforthforthforthforthforth
foughtfoughtfoughtfoughtfoughtfoughtfoughtfoughtfoughtfought
freedomfreedomfreedomfreedomfreedomfreedomfreedomfreedomfreedomfreedom
fromfromfromfromfromfromfromfromfromfrom
fromfromfromfromfromfromfromfromfromfrom
fullfullfullfullfullfullfullfullfullfull
gavegavegavegavegavegavegavegavegavegave
gavegavegavegavegavegavegavegavegavegave
governmentgovernmentgovernmentgovernmentgovernmentgovernmentgovernmentgovernmentgovernmentgovernment
greatgreatgreatgreatgreatgreatgreatgreatgreatgreat
greatgreatgreatgreatgreatgreatgreatgreatgreatgreat
greatgreatgreatgreatgreatgreatgreatgreatgreatgreat
ground.ground.ground.ground.ground.ground.ground.ground.ground.ground.
hallowhallowhallowhallowhallowhallowhallowhallowhallowhallow
havehavehavehavehavehavehavehavehavehave
havehavehavehavehavehavehavehavehavehave
havehavehavehavehavehavehavehavehavehave
havehavehavehavehavehavehavehavehavehave
havehavehavehavehavehavehavehavehavehave
herehereherehereherehereherehereherehere
herehereherehereherehereherehereherehere
herehereherehereherehereherehereherehere
herehereherehereherehereherehereherehere
herehereherehereherehereherehereherehere
here,here,here,here,here,here,here,here,here,here,
here,here,here,here,here,here,here,here,here,here,
here.here.here.here.here.here.here.here.here.here.
highlyhighlyhighlyhighlyhighlyhighlyhighlyhighlyhighlyhighly
honoredhonoredhonoredhonoredhonoredhonoredhonoredhonoredhonoredhonored
inininininininininin
inininininininininin
inininininininininin
inininininininininin
increasedincreasedincreasedincreasedincreasedincreasedincreasedincreasedincreasedincreased
isisisisisisisisisis
isisisisisisisisisis
isisisisisisisisisis
itititititititititit
it,it,it,it,it,it,it,it,it,it,
largerlargerlargerlargerlargerlargerlargerlargerlargerlarger
lastlastlastlastlastlastlastlastlastlast
littlelittlelittlelittlelittlelittlelittlelittlelittlelittle
live.live.live.live.live.live.live.live.live.live.
liveslivesliveslivesliveslivesliveslivesliveslives
livinglivinglivinglivinglivinglivinglivinglivinglivingliving
living,living,living,living,living,living,living,living,living,living,
longlonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglong
measuremeasuremeasuremeasuremeasuremeasuremeasuremeasuremeasuremeasure
menmenmenmenmenmenmenmenmenmen
men,men,men,men,men,men,men,men,men,men,
metmetmetmetmetmetmetmetmetmet
mightmightmightmightmightmightmightmightmightmight
nationnationnationnationnationnationnationnationnationnation
nationnationnationnationnationnationnationnationnationnation
nation,nation,nation,nation,nation,nation,nation,nation,nation,nation,
nation,nation,nation,nation,nation,nation,nation,nation,nation,nation,
nation,nation,nation,nation,nation,nation,nation,nation,nation,nation,
nevernevernevernevernevernevernevernevernevernever
newnewnewnewnewnewnewnewnewnew
newnewnewnewnewnewnewnewnewnew
noblynoblynoblynoblynoblynoblynoblynoblynoblynobly
nornornornornornornornornornor
notnotnotnotnotnotnotnotnotnot
notnotnotnotnotnotnotnotnotnot
notnotnotnotnotnotnotnotnotnot
notnotnotnotnotnotnotnotnotnot
notnotnotnotnotnotnotnotnotnot
note,note,note,note,note,note,note,note,note,note,
ofofofofofofofofofof
ofofofofofofofofofof
ofofofofofofofofofof
ofofofofofofofofofof
ofofofofofofofofofof
onononononononononon
onononononononononon
orororororororororor
orororororororororor
ourourourourourourourourourour
ourourourourourourourourourour
people,people,people,people,people,people,people,people,people,people,
people,people,people,people,people,people,people,people,people,people,
people,people,people,people,people,people,people,people,people,people,
perishperishperishperishperishperishperishperishperishperish
placeplaceplaceplaceplaceplaceplaceplaceplaceplace
poorpoorpoorpoorpoorpoorpoorpoorpoorpoor
portionportionportionportionportionportionportionportionportionportion
powerpowerpowerpowerpowerpowerpowerpowerpowerpower
properproperproperproperproperproperproperproperproperproper
propositionpropositionpropositionpropositionpropositionpropositionpropositionpropositionpropositionproposition
ratherratherratherratherratherratherratherratherratherrather
rather,rather,rather,rather,rather,rather,rather,rather,rather,rather,
remainingremainingremainingremainingremainingremainingremainingremainingremainingremaining
rememberrememberrememberrememberrememberrememberrememberrememberrememberremember
resolveresolveresolveresolveresolveresolveresolveresolveresolveresolve
restingrestingrestingrestingrestingrestingrestingrestingrestingresting
saysaysaysaysaysaysaysaysaysay
scorescorescorescorescorescorescorescorescorescore
sense,sense,sense,sense,sense,sense,sense,sense,sense,sense,
sevensevensevensevensevensevensevensevensevenseven
shallshallshallshallshallshallshallshallshallshall
shallshallshallshallshallshallshallshallshallshall
shallshallshallshallshallshallshallshallshallshall
shouldshouldshouldshouldshouldshouldshouldshouldshouldshould
sosososososososososo
sosososososososososo
struggledstruggledstruggledstruggledstruggledstruggledstruggledstruggledstruggledstruggled
taketaketaketaketaketaketaketaketaketake
tasktasktasktasktasktasktasktasktasktask
testingtestingtestingtestingtestingtestingtestingtestingtestingtesting
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thatthatthatthatthatthatthatthatthatthat
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
thethethethethethethethethethe
theirtheirtheirtheirtheirtheirtheirtheirtheirtheir
thesethesethesethesethesethesethesethesethesethese
thesethesethesethesethesethesethesethesethesethese
theytheytheytheytheytheytheytheytheythey
theytheytheytheytheytheytheytheytheythey
theytheytheytheytheytheytheytheytheythey
thisthisthisthisthisthisthisthisthisthis
thisthisthisthisthisthisthisthisthisthis
thisthisthisthisthisthisthisthisthisthis
this.this.this.this.this.this.this.this.this.this.
thosethosethosethosethosethosethosethosethosethose
thusthusthusthusthusthusthusthusthusthus
totototototototototo
totototototototototo
totototototototototo
totototototototototo
totototototototototo
totototototototototo
totototototototototo
totototototototototo
underunderunderunderunderunderunderunderunderunder
unfinishedunfinishedunfinishedunfinishedunfinishedunfinishedunfinishedunfinishedunfinishedunfinished
usususususususususus
usususususususususus
usususususususususus
vainvainvainvainvainvainvainvainvainvain
war,war,war,war,war,war,war,war,war,war,
war.war.war.war.war.war.war.war.war.war.
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
wewewewewewewewewewe
whatwhatwhatwhatwhatwhatwhatwhatwhatwhat
whatwhatwhatwhatwhatwhatwhatwhatwhatwhat
whetherwhetherwhetherwhetherwhetherwhetherwhetherwhetherwhetherwhether
whichwhichwhichwhichwhichwhichwhichwhichwhichwhich
whichwhichwhichwhichwhichwhichwhichwhichwhichwhich
whowhowhowhowhowhowhowhowhowho
whowhowhowhowhowhowhowhowhowho
whowhowhowhowhowhowhowhowhowho
willwillwillwillwillwillwillwillwillwill
workworkworkworkworkworkworkworkworkwork
worldworldworldworldworldworldworldworldworldworld
yearsyearsyearsyearsyearsyearsyearsyearsyearsyears