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...
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.
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
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative...
Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative numbers from a stack of int objects. If the original stack contained the integers 30, –15, 20, –25 (top of stack), the new stack should contain the integers 30, 20.
Assignment Description: Develop and test an Intel 8086 assembly program that reads two decimal numbers x...
Assignment Description: Develop and test an Intel 8086 assembly program that reads two decimal numbers x and y. Your program should display the result of their: 1) Addition: x+y 2) Subtraction: x-y 3) Multiplication: x*y 4) Division: x / y Notes:  x and y are two-digit decimal numbers (i.e. 0-99).  The program should display an error message if the value of y is zero, in the case of division.  You can assume only positive numbers but you...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT