#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
#define MAX_WORD_LENGTH (255)
typedef struct _TSynonym
{
char sourceWord[MAX_WORD_LENGTH];
char replaceWord[MAX_WORD_LENGTH];
} * TSynonym;
#define SYNONYM_SIZE (sizeof(struct _TSynonym))
typedef struct _TWordList
{
TSynonym synonyms;
int count;
} * TWordList;
typedef struct _TWord
{
char word[MAX_WORD_LENGTH];
int wordSize;
} * TWord;
#define WORD_SIZE ( sizeof(struct _TWord))
//returns the index if found, -1 otherwise
int FindWordInWordList(TWordList wordList, char * wordToFind)
{
int iReturn = -1;
int n = wordList->count;
for (int iLoop=0; iLoop<n; iLoop++)
{
if (strcmp(wordList->synonyms[iLoop].sourceWord,wordToFind)==0)
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
int main() {
int numSynonyms;
int numWords;
cout << " Synonyms :>";
cin >> numSynonyms;
TSynonym synonyms = (TSynonym) malloc(numSynonyms * SYNONYM_SIZE);
for (int iLoop=0; iLoop<numSynonyms; iLoop++)
{
cin >> synonyms[iLoop].sourceWord;
cin >> synonyms[iLoop].replaceWord;
cout << " Replacing " << synonyms[iLoop].sourceWord << " with " << synonyms[iLoop].replaceWord << endl;
}
struct _TWordList wordList;
wordList.synonyms = synonyms;
wordList.count = numSynonyms;
cout << " Words :>";
cin >> numWords;
TWord words = (TWord) malloc(numWords * WORD_SIZE);
for (int iLoop=0; iLoop<numWords; iLoop++)
{
cin >> words[iLoop].word;
words[iLoop].wordSize = strlen(words[iLoop].word)+1;
cout << words[iLoop].word << endl;
int iReturn = FindWordInWordList(&wordList,words[iLoop].word);
if (iReturn>-1)
{
strcpy(words[iLoop].word,synonyms[iReturn].replaceWord);
words[iLoop].wordSize = strlen(words[iLoop].word)+1;
}
}
for (int iLoop=0; iLoop<numWords; iLoop++)
{
cout << words[iLoop].word << " ";
}
{
return 0;
}}
Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced.
Ex: If the input is:
3 automobile car manufacturer maker children kids
15 The automobile manufacturer recommends car seats for children if the automobile doesn't already have one.
then the output is:
The car maker recommends car seats for kids if the car doesn't already have one.
The program above is my work, I just need help to know what I did wrong. Someone please help