Knowee
Questions
Features
Study Tools

Write a program that asks the user to input two strings: an input string and a"pattern" string. Both strings contain only lowercase alphabet, and the"pattern" string is shorter than the input string. The program finds and countsnumber of occurrences of the "pattern" string in the input string, assuming thatthe number of occurrences is less than 10. The program replaces the firstcharacter of the first appearance of the "pattern" string in the input string by '1'and the first character of the second appearance of the "pattern" string in theinput string by '2' and so on so forth. Finally, the program outputs two strings:the original input string and the modified string, a string in a line, ended withnewline.

Question

Write a program that asks the user to input two strings: an input string and a"pattern" string. Both strings contain only lowercase alphabet, and the"pattern" string is shorter than the input string. The program finds and countsnumber of occurrences of the "pattern" string in the input string, assuming thatthe number of occurrences is less than 10. The program replaces the firstcharacter of the first appearance of the "pattern" string in the input string by '1'and the first character of the second appearance of the "pattern" string in theinput string by '2' and so on so forth. Finally, the program outputs two strings:the original input string and the modified string, a string in a line, ended withnewline.

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

Solution

Here is a Python program that accomplishes the task:

def find_and_replace(input_string, pattern_string):
    count = 0
    modified_string = input_string
    while pattern_string in modified_string and count < 10:
        count += 1
        index = modified_string.find(pattern_string)
        modified_string = modified_string[:index] + str(count) + modified_string[index+1:]
    return input_string, modified_string

input_string = input("Enter the input string: ")
pattern_string = input("Enter the pattern string: ")

original_string, modified_string = find_and_replace(input_string, pattern_string)

print(original_string)
print(modified_string)

This program first defines a function find_and_replace that takes an input string and a pattern string. It initializes a count to 0 and creates a copy of the input string to be the modified string. It then enters a loop that continues as long as the pattern string is in the modified string and the count is less than 10. In each iteration of the loop, it increments the count, finds the index of the first occurrence of the pattern string in the modified string, and replaces the character at that index with the current count. After the loop, it returns the original input string and the modified string.

The program then asks the user to input the input string and the pattern string, calls the find_and_replace function with these strings, and prints the original and modified strings.

This problem has been solved

Similar Questions

In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.NOTE: String letters are case-sensitive.Input FormatThe first line of input contains the original string. The next line contains the substring.ConstraintsEach character in the string is an ascii character.Output FormatOutput the integer number indicating the total number of occurrences of the substring in the original string.Sample InputABCDCDCCDCSample Output2ConceptSome string processing examples, such as these, might be useful.There are a couple of new concepts:In Python, the length of a string is found by the function len(s), where is the string.To traverse through the length of a string, use a for loop:for i in range(0, len(s)): print (s[i])A range function is used to loop over some length:range (0, 5)Here, the range loops over to . is excluded.

Problem StatementBob loves playing a string-matching game where he tries to match two strings based on a specific pattern. In this game, players input two strings, and the game determines whether they match according to a set of rules. Here are the rules of the game:A '*' in the first string represents zero or more characters.A '?' in the first string represents exactly one character.Any other character in the first string must match the corresponding character in the second string.Bob has requested your assistance in completing the game mentioned above.Input format :The first line of input consists of a string str1 containing the characters along with the symbols - ? and *.The second line consists of the string str2, without any symbols.Output format :The first line displays the "Second string: " followed by str2 as string, representing the second input string.The second line displays the following format:"The strings match" if the first string matches the second according to the pattern rules."The strings do not match" if the first string does not match the second according to the pattern rules.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ length of the string (str1, str2) ≤ 15Sample test cases :Input 1 :i?mneoiamneoOutput 1 :Second string: iamneoThe strings matchInput 2 :i?miaamOutput 2 :Second string: iaamThe strings do not matchInput 3 :i*mn?oiaamneoOutput 3 :Second string: iaamneoThe strings matchInput 4 :a?b?c?d?e?f?ghaxbxcydzefffghOutput 4 :Second string: axbxcydzefffghThe strings matchInput 5 :a*cacOutput 5 :Second string: acThe strings matchNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.

Write a program that accepts an input string from the user and prints the total number of alphabet characters (A-z) in the string to the output.

Write a program to print the duplicate characters in the given stringInput FormatGiven a stringOutput Formatprint the duplicate characters separated by space ':'Constraint :1 <= string length <= 100Note: If more than one occurred then print the first occurrencethe string might have upper case letter that should be considered as lowercase

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.