9.4. Modifying Text

We can loop through the string and modify it using find and slice (substring).

Google has been digitizing books. But, sometimes the digitizer makes a mistake and uses 1 for i. The following program replaces 1 with i everywhere in the string. Now you might not want to really replace every 1 with i, but don’t worry about that right now. It uses a while loop since we don’t know how many times it will need to loop.

Activity: CodeLens 9.4.1 (Change_Ones)

You don’t have to replace just one character. You could replace every instance of a word with another word. For example, what if you spelled a word wrong and wanted to change it everywhere?

Activity: CodeLens 9.4.2 (Change_Word)

Can you loop through and encode a string to hide the contents of the message?

Activity: CodeLens 9.4.3 (Encode_String)

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

The program below decodes an encoded message, but the lines are mixed up. Put the lines in the right order with the right indentation.

Write the code to replace every 0 with o in the given string ‘The 0wl h00ts l0udly’.

Show Comments

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted 1 of 7 activities on this page