repl.it
@Murzikal/

Find and Replaxe

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Nicholas Khil
CS 8 PD 7
#12
find and replace program
Last modified: 12/6/2019
"""

run = 1

while run == 1:
    sentence = input("Please enter a simple sentence (or q to quit): ")
    if sentence == "q" or sentence == "Q":
        run = 0
        continue

    new = ""
    numFound = 0
    keep = ""
    fString = input("Please enter the string to be found: ")
    nString = input("Please enter the string to replace it: ")

    if fString == "":
        print("Not found :(")
        continue
    
    if fString in sentence:
        for i in range(len(sentence)):
            if sentence[i] == fString[0]:
                if sentence[i:i+(len(fString))] == fString:                    
                    numFound += 1
                    sentence = sentence[:i] + nString + sentence[i+(len(fString))+1:]
                    print(new, i, nString)
    else:
        print("Not found :(")
        continue
    
    print("\n Your new sentence is:", sentence)
    print(" " + fString, "was found", numFound, "time(s)")
    
?