Last Updated: 31 Dec, 2020

Convert String

Easy
Asked in companies
SAP LabsPayUThales

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.
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

Approaches

01 Approach

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();

02 Approach

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 by changing the ASCII value.

 

Steps:

  • Change the string to a character array.
  • Run a loop from index 0. Inside the loop, check for the first element if it's a lowercase letter then change it to uppercase(by subtracting 32 from it).
  • Now check for other elements (except first). If the character just before the alphabet is a space and its ASCII value is greater than 90, then it is a lowercase letter, so change it to uppercase by subtracting 32 from the character.
  • Convert the character array to string by using the toString function.
  • Finally, return the string.