Question

In: Computer Science

P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For...

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.

Solutions

Expert Solution

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

  • prompt user for a string
  • create a an empty string to store modified version of string which havent any duplicates
  • Using a for loop iterate through each charcter in the string
  • In the loop,check if charcter is present before the current character.If yes do nothing ,else append current character to new string

  

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


Related Solutions

In java P4.7 Following Section 4.9, develop a program that reads a string and removes all...
In java 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...
In java P4.6 Following Section 4.9 develop a program that reads text and displays the average...
In java P4.6 Following Section 4.9 develop a program that reads text and displays the average number of words in each sentence. Assume words are separated by spaces, and a sentence ends when a word ends in a period. Start small and just print the first word. Then print the first two words. Then print all words in the first sentence. Then print the number of words in the first sentence. Then print the number of words in the first...
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates...
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns "Empty"...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a program that removes a target item (a string) from a list of strings, and...
Write a program that removes a target item (a string) from a list of strings, and that will print out the list without the item. Do not use built-in functions other than input, print, and append. Instead, you may generate a new list that does not include the target item and you can use the append() function. Your program should read two inputs: the target and the list. For example, if the input is “apple” and the list is [‘berry’,...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT