In C++ Write a function ReadMisspelledWords to read in pairs of misspellings and correct spellings. It should have the following parameters in the order provided:
In C++
Write a function ReadMisspelledWords to read in pairs of misspellings and correct spellings. It should have the following parameters in the order provided:
a string filename
a 2D array of strings misspelledWords[][2]
the number of rows in the array misspelledWord
an integer startIndex.
Your function should return the total number of word pairs in the array misspelledWords. Your function should open the file identified by filename and read lines from the file line by line. The file will have the following format:
misspelledWords.txt
chickn chicken
chicke chicken
chcken chicken
fsh fish
fishe fish
bubbls bubbles
bubles bubbles
per pear
peart pear
The file will be a tab separated values (.txt) file. You can identify tabs by the \t character. The first value on each line is a misspelled word, and the second value on each line is the correct spelling of that word. For example, on the first line chickn is followed by a \t and then the correct spelling chicken.
You should read these values into the 2D array so that the first column contains misspellings and the second column contains the correct spelling. For the file above and startIndex = 0, your array misspelledWords should look like this, and your function should return 9 (for 9 word pairs in the array):
[
["chickn", "chicken"],
["chicke", "chicken"],
["chcken", "chicken"],
["fsh", "fish"],
["fishe", "fish"],
["bubbls", "bubbles"],
["bubles", "bubbles"],
["per", "pear"],
["peart", "pear"]
]
startIndex identifies the row in the array at which you should begin to add the words in the file. If after calling the function a first time I called it again with the below file and startIndex= 9:
moreMisspelledWords.tsv
ocen ocean
ocan ocean
Then I would expect my array to look like this. Your function should return 11 for 11 word pairs in the array.
[
[chickn, chicken],
[chicke, chicken],
[chcken, chicken],
[fsh, fish],
[fishe, fish],
[bubbls, bubbles],
[bubles, bubbles],
[per, pear],
[peart, pear],
[ocen, ocean],
[ocan, ocean]
]
We will call your function multiple times with different files and the same array to make sure you append new words correctly!
Edge cases:
if the file does not exist, return -1
if the starting row of the array is greater than the total number of rows in the array, return -1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started