Knowee
Questions
Features
Study Tools

Your goal is to write a program that will ask the user for a long string and then a substring of that long string. The program then will report information on the long string and the substring. Finally it will prompt the user for a replacement string and show what the long string would be with the substring replaced by the replacement string. Each of the lines below can be produced by using one or more String methods - look to the String method slides on the course website to help you with this project.A few sample transcripts of your code at work are below - remember that your code should end with a newline as in the textbook examples and should produce these transcripts exactly if given the same input. Portions in bold indicate where the user has input a value.One run of your program might look like this:Enter a long string: The quick brown fox jumped over the lazy dogEnter a substring: jumpedLength of your string: 44Length of your substring: 6Starting position of your substring: 20String before your substring: The quick brown fox String after your substring: over the lazy dogEnter a position between 0 and 43: 18The character at position 18 is xEnter a replacement string: leapedYour new string is: The quick brown fox leaped over the lazy dogGoodbye!A second run with different user input might look like this:Enter a long string: Friends, Romans, countrymen, lend me your earsEnter a substring: tryLength of your string: 46Length of your substring: 3Starting position of your substring: 21String before your substring: Friends, Romans, counString after your substring: men, lend me your earsEnter a position between 0 and 45: 21The character at position 21 is tEnter a replacement string: catchYour new string is: Friends, Romans, councatchmen, lend me your earsGoodbye!

Question

Your goal is to write a program that will ask the user for a long string and then a substring of that long string. The program then will report information on the long string and the substring. Finally it will prompt the user for a replacement string and show what the long string would be with the substring replaced by the replacement string. Each of the lines below can be produced by using one or more String methods - look to the String method slides on the course website to help you with this project.A few sample transcripts of your code at work are below - remember that your code should end with a newline as in the textbook examples and should produce these transcripts exactly if given the same input. Portions in bold indicate where the user has input a value.One run of your program might look like this:Enter a long string: The quick brown fox jumped over the lazy dogEnter a substring: jumpedLength of your string: 44Length of your substring: 6Starting position of your substring: 20String before your substring: The quick brown fox String after your substring: over the lazy dogEnter a position between 0 and 43: 18The character at position 18 is xEnter a replacement string: leapedYour new string is: The quick brown fox leaped over the lazy dogGoodbye!A second run with different user input might look like this:Enter a long string: Friends, Romans, countrymen, lend me your earsEnter a substring: tryLength of your string: 46Length of your substring: 3Starting position of your substring: 21String before your substring: Friends, Romans, counString after your substring: men, lend me your earsEnter a position between 0 and 45: 21The character at position 21 is tEnter a replacement string: catchYour new string is: Friends, Romans, councatchmen, lend me your earsGoodbye!

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that accomplishes the task:

# Get the long string from the user
long_string = input("Enter a long string: ")

# Get the substring from the user
substring = input("Enter a substring: ")

# Print the length of the long string and the substring
print("Length of your string: ", len(long_string))
print("Length of your substring: ", len(substring))

# Find and print the starting position of the substring in the long string
start_pos = long_string.find(substring)
print("Starting position of your substring: ", start_pos)

# Print the parts of the long string before and after the substring
print("String before your substring: ", long_string[:start_pos])
print("String after your substring: ", long_string[start_pos + len(substring):])

# Get a position from the user and print the character at that position
pos = int(input("Enter a position between 0 and " + str(len(long_string) - 1) + ": "))
print("The character at position ", pos, " is ", long_string[pos])

# Get a replacement string from the user and replace the substring with it in the long string
replacement = input("Enter a replacement string: ")
new_string = long_string.replace(substring, replacement)
print("Your new string is: ", new_string)

print("Goodbye!")

This program first gets a long string and a substring from the user. It then prints the lengths of these strings and the starting position of the substring in the long string. It also prints the parts of the long string before and after the substring. The user is then asked to enter a position, and the character at that position is printed. Finally, the user is asked to enter a replacement string, and the substring in the long string is replaced with this new string. The resulting string is then printed.

This problem has been solved

Similar Questions

Write a program to find the substring of a given string.

20.5 LAB: Longest stringWrite a program that takes two strings and outputs the longer string, followed by "is longer than", and the shorter string. If the strings have the same length then output the strings followed by "have the same length".Ex. If the input is:almond pistachiothe output is:pistachio is longer than almond

Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.

Given a string s, find the length of the longest substring without repeating characters.

Description:Imagine you're developing a text processing tool that facilitates replacing specific characters in a primary string. Your task is to write a program that accomplishes this efficiently.Input:The first line should prompt the user to enter the primary string ('PS').The second line should ask for the character to be replaced ('c').The third line should request the replacement string ('s').Output:Display the resulting string after replacing each occurrence of 'c' with 's'.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.