Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Sample Examples
3.
Approach 
3.1.
Implementation in C++ 
3.1.1.
Time Complexity
3.1.2.
Space Complexity
4.
Frequently Asked Questions
4.1.
What is STL?
4.2.
What are the STL's main components?
4.3.
How do you use STL?
4.4.
Is STL useful?
4.5.
Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Convert Given Array of Strings to a Camel Case Format Sentence

Author Saksham Gupta
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Often we tend to overlook the simple problems and start practicing the most asked interview questions directly. This might work but doesn’t guarantee success. Today we will be discussing one such problem, namely convert given array of strings to a camel case format sentence.

Introductory Image

In this problem, we will be given an array of strings, and we have to convert them into a camel case string. Let’s discuss the problem in detail.

Problem Statement

We have been given an array ‘ARR’ of strings, and we have to convert them into camel case and print a sentence out of that. But first, let’s discuss what we mean by the camel case.

For example, the word ‘coding ninjas’ will be written as ‘CodingNinjas’, i.e., space between words is removed, and then the next word is capitalized.

Sample Examples

Input

Input

Output

Output

Explanation

Here, we have transformed the first and fourth strings to camel case, capitalized the first character of every other string, and printed the final merged sentence.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Approach 

Intuition for the problem is very straightforward. We will be traversing the array word by word and will be storing the answer in a string, let’s say ‘F_STRING’. When we traverse a single word, we will first capitalize the first character of it. Then if we encounter any space, we will be capitalizing the next character and adding it to our ‘F_STRING.’ (example: “coding ninjas” -> CodingNinjas). Let’s put these words into code now.

Implementation in C++ 

#include <iostream>
#include <vector>
#include <string>
using namespace std;
 
// Function to convert the array of strings in a camelcase sentence.
string camelCase(vector<string> &arr)
{
   // Final sentence will be stored in this.
   string fString = "";
 
   int i = 0;
   while (i < arr.size())
   {
       // Add space after every string.
       if (fString.length() != 0)
       {
           fString += ' ';
       }
 
       // Insert the first character of particular word.
       fString.push_back(toupper(arr[i][0]));
       int j = 1;
       while (j < arr[i].length())
       {
           // If there is a space, this means we have to convert the next letter to capital (so that it can be in camel case).
           if (arr[i][j] == ' ')
           {
               fString.push_back(toupper(arr[i][j + 1]));
               j++;
           }
 
           // Otherwise, we will simply add that character to our final sentence in lower case.
           else
           {
               fString.push_back(tolower(arr[i][j]));
           }
           j++;
       }
 
       i++;
   }
 
   // Return the final sentence.
   return fString;
}
 
// Driver program
int main()
{
   vector<string> arr{
       "coding ninjas",
       "is", "my",
       "first love"};
 
   cout << camelCase(arr);
 
   return 0;
}


Output

CodingNinjas Is My FirstLove


Time Complexity

O(M * N), where ‘N’ is the length of the array and ‘M’ is the average length of all the words in the array. As we are first looping over the array of length ‘N’ and inside the loop, we are looping over a particular word with average length ‘M.’

Space Complexity

O(M * N), where ‘N’ is the length of the array and ‘M’ is the average length of all the words in the array.

As we are using a string to store the final sentence.

Also see, Morris Traversal for Inorder and Rabin Karp Algorithm

Check out this problem - Count Inversions

Frequently Asked Questions

What is STL?

It's a sophisticated and powerful library of template classes and template functions that implement a wide range of common data structures and algorithms, and it's included in the C++ Standard Library.
 

What are the STL's main components?

The most commonly used components of the STL will be containers and container adaptors (both are objects that can hold other objects), iterators (generalized pointers that point at items in containers), and algorithms (that work on containers through iterators).
 

How do you use STL?

By including the necessary header files to allow access to the parts of the STL you need, declaring objects of the appropriate container, iterator, and function types, and then using member functions and/or algorithms to perform whatever tasks your application requires, as appropriate.
 

Is STL useful?

Because the STL represents the concept of reusable software components and provides ready-to-use solutions to a wide range of programming issues. It's also extensible in the sense that any programmer can create new software (containers and algorithms, for example) that "fits in" with the STL and interacts with the STL's existing components, as long as the programmer follows the appropriate design guidelines.
 

Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?

Yes, Coding Ninjas Studio allows you to practice coding as well as answer frequently asked interview questions. The more we practice, the more likely we are to acquire a job at our dream company.

Conclusion 

In this article, we have extensively discussed a coding problem in which we have to convert a given array of strings to a Camel Case format sentence using string traversals. We have seen a simple yet effective approach for solving the problem along with the implementation in C++.

If you think this blog has helped you enhance your knowledge about the above question, and if you would like to learn more, check out our articles 

🔥 How to Print an Array in CPP
 

🔥 Count Inversions in An Array
 

🔥 Array Interview Questions
 

🔥 Check if Two Arrays are Equal or Not
 

Recommended problems -

 

Visit our website to read more such blogs. Make sure you enroll in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. 

Please upvote our blog to help other ninjas grow.

Happy Learning!

Previous article
Count Valleys
Next article
Finding the Longest Palindromic Substring using Manacher’s Algorithm
Live masterclass