Ninja wants to print a book of stories. He created a doc file and sent it to his editor to make some edits. But the file got corrupted due to some reasons and made changes in the original file. Ninja did not have a duplicate file of the same, so he wants to correct the same file. He found that the file has been changed in such a way that all the spaces have been removed from the file and the first letter after each space that used to be has been changed to the equivalent uppercase characters.Example:If the corrupted file looks like “CodingNinjasIsACodingPlatform”, then the original file was: “coding ninjas is a coding platform”.Ninja needs to change the corrupted file to the original file.Note:You need to convert all the uppercase characters to lowercase characters, and you need to add a single space between every two words.
Question
Ninja wants to print a book of stories. He created a doc file and sent it to his editor to make some edits. But the file got corrupted due to some reasons and made changes in the original file. Ninja did not have a duplicate file of the same, so he wants to correct the same file. He found that the file has been changed in such a way that all the spaces have been removed from the file and the first letter after each space that used to be has been changed to the equivalent uppercase characters.Example:If the corrupted file looks like “CodingNinjasIsACodingPlatform”, then the original file was: “coding ninjas is a coding platform”.Ninja needs to change the corrupted file to the original file.Note:You need to convert all the uppercase characters to lowercase characters, and you need to add a single space between every two words.
Solution
To solve this problem, you can follow these steps:
- Initialize an empty string to store the result.
- Iterate over each character in the corrupted file string.
- If the character is uppercase, convert it to lowercase and add a space before it in the result string.
- If the character is lowercase, simply add it to the result string.
- Return the result string.
Here is a Python solution for the problem:
def correct_file(corrupted_file):
result = ""
for char in corrupted_file:
if char.isupper():
result += " " + char.lower()
else:
result += char
return result.lstrip() # remove leading space
corrupted_file = "CodingNinjasIsACodingPlatform"
print(correct_file(corrupted_file)) # prints "coding ninjas is a coding platform"
This function works by checking each character in the input string. If the character is uppercase, it adds a space before it and converts it to lowercase. If the character is lowercase, it adds it to the result string as is. The lstrip() function is used to remove the leading space that is added before the first word.
Similar Questions
Keep the meaning the same. Make sure the re-written content's number of characters is exactly the same as the original text's number of characters. Do not alter the original structure and formatting outlined in any way. Only give me the output and nothing else.
Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below.
Write a program to replace all spaces from text with - (dash)from the file podar.txt.
Ali, a documentation specialist, is reviewing a document. Before finalizing the document, he checks for extra spaces, paragraph marks, and other hidden formatting symbols. Here, Ali is checking for _____.Select one:a.Unicode charactersb.nonprinting charactersc.special charactersd.ASCII characters
When using Find and Replace, you want to ensure that your Replace With text uses upper and lower case letters exactly as intended. How can you ensure this?In the Replace With field, type everything in lowercase.In the Replace With field, type everything in uppercase.This will happen by default.
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.