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++ program of the above approach #include <bits/stdc++.h> using namespace std;
// Function to convert the given array // of strings into a sentence in the // Camel Case formatting string convertCase(vector<string> arr, int N)
{ // Stores the final sentence
string ans = "" ;
// Loop to iterate over the array
for ( int i = 0; i < N; i++) {
// If the current word is not the 1st
// word, insert space
if (ans.size() > 0) {
ans += ' ' ;
}
// Insert the first character of arr[i]
ans += toupper (arr[i][0]);
// Loop to iterate over current array element
for ( int j = 1; j < arr[i].size(); j++) {
// If a space is found,
// the next character
// should be in upper case
if (arr[i][j] == ' ' ) {
ans += ' ' ;
ans += toupper (arr[i][j + 1]);
j++;
}
// Otherwise the characters
// must be in the lower case
else {
ans += tolower (arr[i][j]);
}
}
}
// Return Answer
return ans;
} // Driver program int main()
{ vector<string> arr{
"AnNiruddHA Routh" ,
"LOVES" , "to" ,
"COdE everyDAY"
};
int N = arr.size();
cout << convertCase(arr, N);
return 0;
} |
// Java code for the above approach import java.util.*;
class GFG{
// Function to convert the given array // of strings into a sentence in the // Camel Case formatting static String convertCase(String[] arr, int N)
{ // Stores the final sentence
String ans = "" ;
// Loop to iterate over the array
for ( int i = 0 ; i < N; i++)
{
// If the current word is not the 1st
// word, insert space
if (ans.length() > 0 )
{
ans += ' ' ;
}
// Insert the first character of arr[i]
ans += Character.toUpperCase(arr[i].charAt( 0 ));
// Loop to iterate over current array element
for ( int j = 1 ; j < arr[i].length(); j++)
{
// If a space is found,
// the next character
// should be in upper case
if (arr[i].charAt(j) == ' ' )
{
ans += ' ' ;
char t = Character.toUpperCase(
arr[i].charAt(j + 1 ));
ans += t;
j++;
}
// Otherwise the characters
// must be in the lower case
else
{
ans += Character.toLowerCase(
arr[i].charAt(j));
}
}
}
// Return Answer
return ans;
} // Driver code public static void main(String[] args)
{ String[] arr = { "AnNiruddHA Routh" , "LOVES" , "to" ,
"COdE everyDAY" };
int N = arr.length;
System.out.println(convertCase(arr, N));
} } // This code is contributed by Potta Lokesh |
# Python3 program of the above approach # Function to convert the given array # of strings into a sentence in the # Camel Case formatting def convertCase(arr, N) :
# Stores the final sentence
ans = "";
# Loop to iterate over the array
for i in range (N) :
# If the current word is not the 1st
# word, insert space
if ( len (ans) > 0 ) :
ans + = ' ' ;
# Insert the first character of arr[i]
ans + = arr[i][ 0 ].upper();
j = 1
# Loop to iterate over current array element
while j < len (arr[i]) :
# If a space is found,
# the next character
# should be in upper case
if (arr[i][j] = = ' ' ) :
ans + = ' ' ;
ans + = arr[i][j + 1 ].upper();
j + = 1 ;
# Otherwise the characters
# must be in the lower case
else :
ans + = arr[i][j].lower();
j + = 1 ;
# Return Answer
return ans;
# Driver program if __name__ = = "__main__" :
arr = [ "AnNiruddHA Routh" , "LOVES" , "to" , "COdE everyDAY" ]
N = len (arr);
print (convertCase(arr, N));
# This code is contributed by AnkThon
|
// C# code for the above approach using System;
class GFG
{ // Function to convert the given array
// of strings into a sentence in the
// Camel Case formatting
static String convertCase(String[] arr, int N)
{
// Stores the final sentence
String ans = "" ;
// Loop to iterate over the array
for ( int i = 0; i < N; i++)
{
// If the current word is not the 1st
// word, insert space
if (ans.Length > 0)
{
ans += ' ' ;
}
// Insert the first character of arr[i]
ans += char .ToUpper(arr[i][0]);
// Loop to iterate over current array element
for ( int j = 1; j < arr[i].Length; j++)
{
// If a space is found,
// the next character
// should be in upper case
if (arr[i][j] == ' ' )
{
ans += ' ' ;
char t = char .ToUpper(arr[i][j + 1]);
ans += t;
j++;
}
// Otherwise the characters
// must be in the lower case
else
{
ans += char .ToLower(arr[i][j]);
}
}
}
// Return Answer
return ans;
}
// Driver code
public static void Main()
{
String[] arr = { "AnNiruddHA Routh" , "LOVES" , "to" ,
"COdE everyDAY" };
int N = arr.Length;
Console.Write(convertCase(arr, N));
}
} // This code is contributed by gfgking |
<script> // JavaScript program of the above approach
// Function to convert the given array
// of strings into a sentence in the
// Camel Case formatting
const convertCase = (arr, N) => {
// Stores the final sentence
let ans = "" ;
// Loop to iterate over the array
for (let i = 0; i < N; i++) {
// If the current word is not the 1st
// word, insert space
if (ans.length > 0) {
ans += ' ' ;
}
// Insert the first character of arr[i]
ans += (arr[i][0]).toUpperCase();
// Loop to iterate over current array element
for (let j = 1; j < arr[i].length; j++) {
// If a space is found,
// the next character
// should be in upper case
if (arr[i][j] == ' ' ) {
ans += ' ' ;
ans += (arr[i][j + 1]).toUpperCase();
j++;
}
// Otherwise the characters
// must be in the lower case
else {
ans += (arr[i][j]).toLowerCase();
}
}
}
// Return Answer
return ans;
}
// Driver program
let arr = [
"AnNiruddHA Routh" ,
"LOVES" , "to" ,
"COdE everyDAY"
];
let N = arr.length;
document.write(convertCase(arr, N));
// This code is contributed by rakeshsahni </script> |
Anniruddha 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)