In: Computer Science
P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For example, if the input is Mississippi, print Misp. Start small and just print the first letter. Then print the first letter and true if the letter is not duplicated elsewhere, false otherwise. (Look for it in the remaining string, by using the substring and indexOf method). Next, do the same for the first two letters, and print out for each letter whether or not they occur in the substring before and after the letter. Try with a string like oops. Extend to all characters in the string. Have a look at the output when the input is Mississippi. Which character should you not report? At this time, you should have gathered enough experience that you can complete the program.
Here our task is to recieve a string from user and print it without duplicates
You didnt mentioned a specific language to code this so assume any language is fine.For simplicity i am using python.If you need code in a specifc language kindly repost it with mentioning the language you need help with
There are couple of ways to code this,At first we can proceed with the way mentioned in question
To print the first letter of the string ,
string[:1]
Now we have to search if the first letter is ever repeated in string
for that we can use index() method .this method will return the index if it is preseent in string else it will produce a value error
try :
string[1:].index(string[:1])
print('True')
except ValueError:
print('False')
By using thease substring and index() method ,we can find all duplicates
Algorithm
CODE
SAPMLE RUN
Text version of the code to copy-paste
string=input('Enter the string :') #getting user input
new_string='' #new string creation
l=len(string) #finding length of string
for i in range(0,l): #loop that iterate through each
chracter
try :
string[:i].index(string[i]) #checking whether current character is
present upto the index of current
#item from beginning of the string
except ValueError: #if not present
new_string=new_string+string[i] #character is appended
print(new_string) #printing newstring