17.6k views
5 votes
5. Write a program to remove any duplicate letters from a word and display the new word maintaining the original order. For example, bananas would become bans and bookkeeper would become bokepr. You may not use any special unique commands!

1 Answer

4 votes

Answer:

word = input('Enter a single word: ', 's');

n = length(word);

nodupWord = [];

for i = 1:n

dup = false;

c = word(i);

for j = 1:i-1

if word(j) == c

dup = true;

break;

end

end

if ~dup

nodupWord = [nodupWord, c]; %add the non-duplicate char to end

end

end

disp(['Adjusted word: ', nodupWord])

Step-by-step explanation:

The code is in Python.

User Michel Billaud
by
5.7k points