Convert given Array of Strings to a Camel Case format sentence - GeeksforGeeks

12 Dec, 2021
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 EverydayExplanation: 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 formatElse insert all other characters in lower case formatWhenever a word ends, add a space to the string, except for the last word (insert a full stop in this case).
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++
Java
Python3
C#
Javascript
// 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 formattingstring 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 programint 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 approachimport java.util.*; class GFG{ // Function to convert the given array// of strings into a sentence in the// Camel Case formattingstatic 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 codepublic 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 formattingdef 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 programif __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 approachusing 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 stringsAuxiliary Space: O(N*M)
 
rakeshsahni
ankthon
lokeshpotta20
gfgking
sagartomar9927
JP Morgan
Arrays
Greedy
Strings
Arrays
Strings
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Reversal algorithm for array rotation
Program to find sum of elements in a given array
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string