String containing first letter of every word in a given string with spaces

Last Updated : 18 Jul, 2026

Given a string s, the task is to create a string with the first letter of every word in the string. The string s contains lower case English alphabets and its words have a single space between them. 

Examples: 

Input: s = "geeks for geeks"
Output: gfg
Explanation:
First word starts at index 0, take 'g'
After first space, next word starts with 'f'
After second space, next word starts with 'g'
Thus, the resulting string formed is "gfg.

Input: s = "bad is good"
Output: big
Explanation:
First word starts at index 0, take 'b'.
After the first space, the next word starts with 'i'.
After the second space, the next word starts with 'g'.
Thus, the resulting string formed is "big".

Try It Yourself
redirect icon

Using Single Pass Traversal - O(n) Time O(k) Space

The Idea is to Initialize an empty result string and add the first character if it is not a space. Traverse the string and whenever a space is encountered, check if the next character is not a space. If so, append that character to the result as it marks the beginning of a new word. Finally, return the constructed string containing the first letters of all words.

Working of Approach:

  • Initialize an empty string res to store the result.
  • Check the first character of the string; if it is not a space, add it to res.
  • Traverse the string from index 0 to n-1.
  • For every character, if it is a space and the next character is not a space, append the next character to res (start of a new word).
  • After completing the traversal, return the final result string containing first letters of all words.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to find string which has first
// character of each word.
// Back-end complete function template for C++

// Function to find the first alphabet of each word in a given string.
string firstAlphabet(string &s)
{
    // initializing the result string.
    string res = "";

    // appending the first character of the string to the result, if it is not a
    // space.
    if (s[0] != ' ')
        res += s[0];

    // iterating over the characters of the string.
    for (int i = 0; i < s.size(); i++)
    {
        // checking if the current character is a space and the next character is
        // not a space. If true, then appending the next character to the result.
        if (i != s.length() - 1 && s[i] == ' ' && s[i + 1] != ' ')
            res += s[i + 1];
    }

    // returning the result string.
    return res;
}

int main()
{
    string str = "geeks for geeks";
    cout << firstAlphabet(str);  
    return 0;
}
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to find the first alphabet of each word in a given string.
char* firstAlphabet(char* s)
{
    int len = strlen(s);
    char res[len + 1];
    int j = 0;

    // Add first character if not space
    if (s[0] != ' ')
        res[j++] = s[0];

    // Traverse the string
    for (int i = 0; i < len; i++)
    {
        if (i != len - 1 && s[i] == ' ' && s[i + 1] != ' ')
        {
            res[j++] = s[i + 1];
        }
    }

    res[j] = '\0';

    // Allocate memory for result
    char *result = (char*)malloc((j + 1) * sizeof(char));
    strcpy(result, res);

    return result;
}

// Driver code
int main()
{
    char str[] = "geeks for geeks";
    printf("%s", firstAlphabet(str));
    return 0;
}
Java
import java.util.*;

// Function to find the first alphabet of each word in a given string.
public class GFG {
    public static String firstAlphabet(String s) {
        
        // initializing the result string.
        String res = "";

        // appending the first character of the string to the result, if it is not a space.
        if (s.charAt(0) != ' ')
            res += s.charAt(0);

        // iterating over the characters of the string.
        for (int i = 0; i < s.length(); i++) {
            
            // checking if the current character is a space and the next character is
            // not a space. If true, then appending the next character to the result.
            if (i != s.length() - 1 && s.charAt(i) == ' ' && s.charAt(i + 1) != ' ')
                res += s.charAt(i + 1);
        }

        // returning the result string.
        return res;
    }

    public static void main(String[] args) {
        String str = "geeks for geeks";
        System.out.println(firstAlphabet(str));
    }
}
Python
def firstAlphabet(s):
    # initializing the result string.
    res = "" 
    
    # appending the first character of the string to the result, if it is not a space.
    if s[0] != ' ':
        res += s[0]
        
    # iterating over the characters of the string.
    for i in range(len(s)):
        # checking if the current character is a space and the next character is not a space. If true, then appending the next character to the result.
        if i != len(s) - 1 and s[i] == ' ' and s[i + 1] != ' ':
            res += s[i + 1]
    
    # returning the result string.
    return res


if __name__ == '__main__':
    s = "geeks for geeks"
    print(firstAlphabet(s))
C#
using System;

class GFG
{
    // Function to find string which has first
    // character of each word.
    // Back-end complete function template for C#

    // Function to find the first alphabet of each word in a given string.
    static string firstAlphabet(string s)
    {
        // initializing the result string.
        string res = "";

        // appending the first character of the string to the result, if it is not a
        // space.
        if (s[0] != ' ')
            res += s[0];

        // iterating over the characters of the string.
        for (int i = 0; i < s.Length; i++)
        {
            // checking if the current character is a space and the next character is
            // not a space. If true, then appending the next character to the result.
            if (i != s.Length - 1 && s[i] == ' ' && s[i + 1] != ' ')
                res += s[i + 1];
        }

        // returning the result string.
        return res;
    }

    static void Main()
    {
        string str = "geeks for geeks";
        Console.WriteLine(firstAlphabet(str));
    }
}
JavaScript
function firstAlphabet(s) {
    // initializing the result string.
    let res = ""; 
    
    // appending the first character of the string 
    // to the result, if it is not a space.
    if (s[0] != ' ') {
        res += s[0];
    }

    // iterating over the characters of the string.
    for (let i = 0; i < s.length; i++) {
        // checking if the current character is a space and the next character is not a space.
        // If true, then appending the next character to the result.
        if (i != s.length - 1 && s[i] === ' ' && s[i + 1] != ' ') {
            res += s[i + 1];
        }
    }

    return res;
}

// Driver code
let str = "geeks for geeks";
console.log(firstAlphabet(str));

Output
gfg

Using Split/Tokenization - O(n) Time O(n) Space

Split the given string into individual words using a built-in split/tokenization library. Then traverse each word and extract its first character. Append these characters to a result string and return the final string.

Working of Approach:

  • Split the input string into words using a library function (like split, stringstream, or strtok).
  • Store the extracted words in a list/array.
  • Traverse each word one by one.
  • For every word, take its first character and append it to the result string.
  • After processing all words, return the constructed result string.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to find the first alphabet of each word in a given string.
string firstAlphabet(string &s)
{
    // creating a stringstream object to split the string into words
    stringstream ss(s);

    string word;

    string res = "";

    // extracting words one by one using stringstream
    while (ss >> word)
    {
        // taking the first character of each word
        // and appending it to the result string
        res += word[0];
    }

    return res;
}
// Driver code
int main()
{
    char input[] = "geeks for geeks";
    string s = input;
    cout << firstAlphabet(s);

    return 0;
}
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to find first alphabet of each word
char* firstAlphabet(char* s)
{
    char *token = strtok(s, " ");
    char *res = (char*)malloc(100 * sizeof(char));
    int j = 0;

    // splitting string and processing each word
    while (token != NULL) {
        res[j++] = token[0];
        token = strtok(NULL, " ");
    }

    res[j] = '\0';
    return res;
}

int main()
{
    char str[] = "geeks for geeks";
    printf("%s", firstAlphabet(str));
    return 0;
}
Java
public class GFG {
    public static String firstAlphabet(String s) {
        String[] words = s.split(" ");
        String res = "";

        // processing each word
        for (String w : words) {
            res += w.charAt(0);
        }

        return res;
    }

    public static void main(String[] args) {
        String s = "geeks for geeks";
        System.out.println(firstAlphabet(s));
    }
}
Python
def firstAlphabet(s):
    words = s.split()
    res = ""

    # processing each word
    for w in words:
        res += w[0]

    return res

s = "geeks for geeks"
print(firstAlphabet(s))
C#
using System;

class GFG {
    static string firstAlphabet(string s) {
        string[] words = s.Split(' ');
        string res = "";

        // processing each word
        foreach (string w in words) {
            res += w[0];
        }

        return res;
    }

    static void Main() {
        string s = "geeks for geeks";
        Console.WriteLine(firstAlphabet(s));
    }
}
JavaScript
function firstAlphabet(s)
{
    let words = s.split(" ");
    let res = "";

    // processing each word
    for (let w of words) {
        res += w[0];
    }

    return res;
}

// Driver Code
let s = "geeks for geeks";
console.log(firstAlphabet(s));

Output
gfg
Comment