C++ Program to Print the First Letter of Each Word of a String
Last Updated :
07 Mar, 2024
String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space.
Examples:
Input: str = "geeks for geeks"
Output: gfg
Input: str = "happy coding"
Output: hc
Source: https://www.geeksforgeeks.org/amazon-interview-set-8-2/
The idea is to traverse each character of string str and maintain a boolean variable, which was initially set as true. Whenever we encounter space we set the boolean variable is true. And if we encounter any character other than space, we will check the boolean variable, if it was set as true as copy that charter to the output string and set the boolean variable as false. If the boolean variable is set false, do nothing.
Algorithm:
1. Traverse string str. And initialize a variable v as true.
2. If str[i] == ' '. Set v as true.
3. If str[i] != ' '. Check if v is true or not.
a) If true, copy str[i] to output string and set v as false.
b) If false, do nothing.
C++
#include<bits/stdc++.h>
using namespace std;
string firstLetterWord(string str)
{
string result = "" ;
bool v = true ;
for ( int i = 0; i < str.length(); i++)
{
if (str[i] == ' ' )
v = true ;
else if (str[i] != ' ' && v == true )
{
result.push_back(str[i]);
v = false ;
}
}
return result;
}
int main()
{
string str = "geeks for geeks" ;
cout << firstLetterWord(str);
return 0;
}
|
Time Complexity: O(n)
Space Complexity: O(1), if space of storing resultant string is taken in account it will be O(n).
Another Approach:
In this approach, we will first split the input string based on the spaces. The spaces in the strings can be matched using a regular expression. The split strings are stored in an array of strings. Then we can simply add the first character of each split string in the result.
C++
#include <bits/stdc++.h>
using namespace std;
string processWords( char *input)
{
char *p;
vector<string> s;
p = strtok (input, " " );
while (p != NULL)
{
s.push_back(p);
p = strtok (NULL, " " );
}
string charBuffer;
for (string values : s)
charBuffer += values[0];
return charBuffer;
}
int main()
{
char input[] = "geeks for geeks" ;
cout << processWords(input);
return 0;
}
|
Time Complexity: O(N + M), where n is the length of the input string and m is the number of words.
Space Complexity: O(M + N)
Method: Using Recursion
C++
#include<bits/stdc++.h>
using namespace std;
string firstLetterWord(string str, int index, string result)
{
if (index == str.length())
return result;
if (str[index] == ' ' )
{
result.push_back(str[index+1]);
}
return firstLetterWord(str, index + 1, result);
}
int main()
{
string str = "geeks for geeks" ;
cout<< firstLetterWord(str, 0, "g" );
return 0;
}
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Please refer complete article on String containing the first letter of every word in a given string with spaces for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...