Convert String

Easy
0/40
Average time to solve is 15m
profile
Contributed by
234 upvotes
Asked in companies
ThalesSAP LabsPayU

Problem statement

You are given a string 'STR'. You have to convert the first alphabet of each word in a string to UPPER CASE.

For example:

If the given string 'STR' = ”I am a student of the third year” so you have to transform this string to ”I Am A Student Of The Third Year"

Note:

'STR' will contains only space and alphabets both uppercase and lowercase. The words will be separated by space.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases.

Then, the ‘T’ test cases follow.

The first and only line of each test case contains the string 'STR' that needs to be transformed.
Output format :
For each test case, print the final string after transformation.

The output of each test case will be printed in a separate line.

Note:

You don’t have to print anything; it has already been taken care of. Just implement the given function. 
Constraints:
1 <= 'T' <= 10
1 <= |STR| <= 10^5

Where |STR| denotes the length of the string.

Time Limit: 1 sec
Sample Input 1 :
3
I love programming
they are playing cricket
good to see you
Sample Output 1 :
I Love Programming
They Are Playing Cricket
Good To See You
Explanation of The Sample Input 1:
For the first test case:
Given string is “I love programming” we will convert every letter after space to uppercase to give the output as ”I Love Programming”.

For the second test case:
Given string is “they are playing cricket” we will convert every letter after space to uppercase to give the output as  “They Are Playing Cricket”. 

For the third test case:
Given string is “good to see you” we will convert every letter after space to uppercase to give the output as “Good To See You”. 
Sample Input 2 :
3
why you are confused
Its a good day to be here
go and do your work
Sample Output 2 :
 Why You Are Confused
 Its A Good Day to Be Here
 Go And Do Your Work
Hint

Try and think how you will know which character needs to be converted to uppercase.

Approaches (2)
Using inbuilt library function

The idea is to traverse the String and check for lowercase letters just after space; that is, you will check the letter after space if it is lowercase, then change it to uppercase.

 

Steps:

  • First, convert this string to StringBuilder RESULT to make changes.
  • Take a boolean variable FLAG and set it to true. This is to check if the current letter needs to be converted to uppercase or not.
  • Now iterate over the string from index zero and check for FLAG.
  • If FLAG is true and the current letter is lowercase, then change it to uppercase by using the inbuilt library function toUpperCase() (this function converts the lowercase letter to uppercase, it will take the character as an argument and will change the value of the uppercase letter) example: toUpperCase(charAt(i)).
  • Now check for white space, and if it says true, then set FLAG value to true, because just after space, we need to convert the alphabet.
  • Else set the flag value to false.
  • Finally, Convert StringBuilder to string by using the toString() function and return simultaneously like RETURN RESULT.toString();
Time Complexity

O(N), Where ‘N’ denotes the string's length.

 

Since we are traversing the string once.

Space Complexity

O(N), Where ‘N’ denotes the string's length.

 

Since we are using string builder to store the string.

Code Solution
(100% EXP penalty)
Convert String
Full screen
Console