In: Computer Science
Please, write this code in c++. Using iostream and cstring library.
Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace.
Note. The program have to use pointer.
Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols.
Output: Formatted text.
example:
input: Can you can the can with can ?
output: Can you can the with ?
/* C++ program to display non duplicate
words from a line of words*/
/* include necessary header files */
#include <iostream>
#include <cstring>
using namespace std;
void removeDuplicate(char * s) {
/*
function to remove duplicate
words from a line of words
@Param: A string pointer
Return:
*/
/* declare & initialize variable(s) */
char * m = s;
char * n = s;
char * x = s;
char word[31];
int index = 0;
int i = 0, length = 0, count = 0;
bool duplicate = false;
// repeat until end of input string
while ( * m != '\0') {
/* initialize */
n = m;
index = 0;
/* copy a word */
while ( * n != ' ' && * n != '\0') {
word[index] = * n;
n++;
index++;
}
/* make end of word by inserting NULL character */
word[index] = '\0';
/* find length of current word */
length = strlen(word);
/* initialize */
count = 0;
duplicate = false;
x = s;
i = 0;
/* search for current word before the
position of current word in input string*/
while (x <= (m - count - 1)) {
/* if charcater is matched */
if (word[i] == * x) {
count++;
i++;
}
/* if charcater is not matched go directly
to next word to search comparision*/
else {
count = 0;
i = 0;
while ( * x != ' ')
x++;
}
/* if current word is found
mark the word as duplicate */
if (count == length) {
duplicate = true;
break;
}
x++;
}
/* if not duplicate then display*/
if (duplicate == false)
cout << word << " ";
/* copy postion which the space after word */
m = n;
/* next postion which is the start of next word */
m++;
}
}
/* Driver method */
int main() {
/* declare variable(s) */
char line[1001];
/* Take input(s) */
cin.getline(line, 1001);;
/* Call function(s) */
removeDuplicate(line);
return 0;
}
___________________________________________________________________
___________________________________________________________________
Case 1:
Can you can the can with can ?
Can you can the with ?
Case 2:
This is This a C++ program C++ !
This is a C++ program !
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.