Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file.
Rules to be followed:
- Words less than or equal to 3 characters need not be scrambled.
- Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg, i.e. letters except first and last can be scrambled in any order.
- Punctuation at the end of the word to be maintained as is i.e. “Surprising, ” could become “Spsirnirug, ” but not “Spsirn, irug”.
- Following punctuation marks are to be supported – Comma Question mark, Full stop, Semicolon, Exclamation.
- Do this for a file and maintain sequences of lines.
On executing the program, it should prompt the user to enter input file name and generate an output file with scrambled text. The output file should be named by appending the word “Scrambled” to the input file name.
Input : MyFile.txt -> Scrambling words is very interesting. Because even if they are scrambled, it doesn't impact our reading. Because we don't read letter by letter, we read the word as a whole. Output : MyFileScrambled.txt -> Srbmnacilg wrods is vrey itrensientg. Bscauee even if tehy are srelabcmd, it dosn'et ipcmat our raidneg. Bacusee we dn'ot raed lteetr by letetr, we raed the wrod as a wolhe.
Below is the implementation :
Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole.
- Python program to extract Email-id from URL text file
- Python | Create an empty text file with current date as its name
- Python | Count occurrences of each word in given text file (Using dictionary)
- NLP | How tokenizing text, sentence, words works
- Different ways of Reading a text file in Java
- reStructuredText | .rst file to HTML file using Python for Documentations
- Text Preprocessing in Python | Set 2
- Text Analysis in Python 3
- Possible Words using given characters in Python
- Python | Tokenize text using TextBlob
- Tokenize text using NLTK in python
- Python | Text to Speech by using pyttsx3
- Python | Merge two text files
- Convert Text to Speech in Python
- Python PIL | ImageDraw.Draw.text()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.