Given an array arr[] of N strings, each containing words with upper or lower case English alphabets, the task is to create a sentence in a Camel Case format using them.
Example:
Input: arr[] = {“AnNiruddHA Routh”, “LOVES”, “to”, “COdE everyDAY””}
Output: AniruddhaRouth Loves To Code Everyday
Explanation: The above sentence is the merged sentence of all the words in the given order in Camel Case.
Input: arr[] = {“I”, “GOT”, “iNtErN”, “at geekSfoRgeekS”}
Output: I Got Intern At Geeksforgeeks
Approach: The given problem can be solved by traversing each word one by one and inserting every character in the Camel case format into a resultant string, as shown in steps below:
- Create an empty string to store the resultant string
- Traverse the array of strings word by word, and for each word:
- If the character is at first index, insert the current character in upper case format
- Else insert all other characters in lower case format
- Whenever a word ends, add a space to the string, except for the last word (insert a full stop in this case).
- Return the resultant string at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string convertCase(vector<string> arr, int N)
{
string ans = "" ;
for ( int i = 0; i < N; i++) {
if (ans.size() > 0) {
ans += ' ' ;
}
ans += toupper (arr[i][0]);
for ( int j = 1; j < arr[i].size(); j++) {
if (arr[i][j] == ' ' ) {
ans += ' ' ;
ans += toupper (arr[i][j + 1]);
j++;
}
else {
ans += tolower (arr[i][j]);
}
}
}
return ans;
}
int main()
{
vector<string> arr{
"AnNiruddHA Routh" ,
"LOVES" , "to" ,
"COdE everyDAY"
};
int N = arr.size();
cout << convertCase(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String convertCase(String[] arr, int N)
{
String ans = "" ;
for ( int i = 0 ; i < N; i++)
{
if (ans.length() > 0 )
{
ans += ' ' ;
}
ans += Character.toUpperCase(arr[i].charAt( 0 ));
for ( int j = 1 ; j < arr[i].length(); j++)
{
if (arr[i].charAt(j) == ' ' )
{
ans += ' ' ;
char t = Character.toUpperCase(
arr[i].charAt(j + 1 ));
ans += t;
j++;
}
else
{
ans += Character.toLowerCase(
arr[i].charAt(j));
}
}
}
return ans;
}
public static void main(String[] args)
{
String[] arr = { "AnNiruddHA Routh" , "LOVES" , "to" ,
"COdE everyDAY" };
int N = arr.length;
System.out.println(convertCase(arr, N));
}
}
|
Python3
def convertCase(arr, N) :
ans = "";
for i in range (N) :
if ( len (ans) > 0 ) :
ans + = ' ' ;
ans + = arr[i][ 0 ].upper();
j = 1
while j < len (arr[i]) :
if (arr[i][j] = = ' ' ) :
ans + = ' ' ;
ans + = arr[i][j + 1 ].upper();
j + = 1 ;
else :
ans + = arr[i][j].lower();
j + = 1 ;
return ans;
if __name__ = = "__main__" :
arr = [ "AnNiruddHA Routh" , "LOVES" , "to" , "COdE everyDAY" ]
N = len (arr);
print (convertCase(arr, N));
|
C#
using System;
class GFG
{
static String convertCase(String[] arr, int N)
{
String ans = "" ;
for ( int i = 0; i < N; i++)
{
if (ans.Length > 0)
{
ans += ' ' ;
}
ans += char .ToUpper(arr[i][0]);
for ( int j = 1; j < arr[i].Length; j++)
{
if (arr[i][j] == ' ' )
{
ans += ' ' ;
char t = char .ToUpper(arr[i][j + 1]);
ans += t;
j++;
}
else
{
ans += char .ToLower(arr[i][j]);
}
}
}
return ans;
}
public static void Main()
{
String[] arr = { "AnNiruddHA Routh" , "LOVES" , "to" ,
"COdE everyDAY" };
int N = arr.Length;
Console.Write(convertCase(arr, N));
}
}
|
Javascript
<script>
const convertCase = (arr, N) => {
let ans = "" ;
for (let i = 0; i < N; i++) {
if (ans.length > 0) {
ans += ' ' ;
}
ans += (arr[i][0]).toUpperCase();
for (let j = 1; j < arr[i].length; j++) {
if (arr[i][j] == ' ' ) {
ans += ' ' ;
ans += (arr[i][j + 1]).toUpperCase();
j++;
}
else {
ans += (arr[i][j]).toLowerCase();
}
}
}
return ans;
}
let arr = [
"AnNiruddHA Routh" ,
"LOVES" , "to" ,
"COdE everyDAY"
];
let N = arr.length;
document.write(convertCase(arr, N));
</script>
|
OutputAnniruddha Routh Loves To Code Everyday
Time Complexity: O(N*M), where M is the average length of a string over all the given strings
Auxiliary Space: O(N*M)