Ninja And Editor

Posted: 20 Feb, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

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.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain a string ‘S’, which denotes the corrupted string that you need to to change.
Output Format:
For each test case, print a single line containing a string denoting the original sentence.

The output for every test case will be printed in a separate line.
Note:
You don't need to print anything, It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
0 <= size of S <= 10000

where ‘T’ is the number of test cases.
where 'S’ is the corrupted string that you need to to change.

Time limit: 1 sec
Approach 1

    The basic approach is to traverse through the complete string of characters and check if the character is in uppercase or lowercase. If it is in uppercase, simply update the character with a space followed by the lowercase conversion of the character.

 

Algorithm: 

  • Traverse through the string of characters and check for characters:
    • If lowercase:
      • Continue
    • Else -> uppercase:
      • Change it with a space followed by lowercase conversion.
  • Return the updated string
  • The corner case in every string is that if the first character is in uppercase, then we need not print a space before it. So, we will check for the first character of the string separately:
    • If lowercase:
      • Continue
    • Else -> uppercase:
      • Change it with lowercase characters.
Try Problem