Print first letter of every word in the string | Practice | GeeksforGeeks

Given a string S, the task is to create a string with the first letter of every word in the string.
 
Example 1:
Input: 
S = "geeks for geeks"
Output: gfg


Example 2:
Input: 
S = "bad is good"
Output: big


Your Task:
You don't need to read input or print anything. Your task is to complete the function firstAlphabet() which takes string S as input parameter and returns a string that contains the first letter of every word in S.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

Constraints:
1 <= |S| <= 105
S contains lower case English alphabets
Words have a single space between them. 
0
radhikamendiratta199411 hours ago
python solution
def firstAlphabet(self, S):
	    if S[0] != ' ':
		    output = S[0]
		for i in range(len(S)):
		    if S[i] ==' ':
		        output += S[i+1]
		return output
+1
sharvilpatil1312 hours ago
JAVA CODE
class Solution {   String firstAlphabet(String S)    {       String res=""+S.charAt(0);       for(int i=0; i<S.length()-1; i++)       {           if(S.charAt(i)==' ' && S.charAt(i+1)!=' ') res=res+S.charAt(i+1);       }       return res;   }};
0
criticaugen3 days ago
#Python Code:
#changing the string into list and checking the values
#code :
 
def firstAlphabet(self, S):    # code here    S=list(S)
    output=""
    for i in range(len(S)):
        if i==0 and S[i].isalpha():
            output+=S[i]
        elif S[i].isalpha() and S[i-1]==" ":
            output+=S[i]
    return output
0
shykatroybdku1994 days ago
String firstAlphabet(String S) {       String string="";       for(int i=0; i<S.length(); i++){           if(S.charAt(i)!=' '){               if(i==0){                   string+=(S.charAt(i));               }else if(S.charAt(i-1)== ' '){                   string+=(S.charAt(i));               }           }       }       return string;
0
abhilaygude1 week ago
class Solution:def firstAlphabet(self, S): l=[] if len(S)>0:     l.append(S[0]) for i in range(len(S)):     if S[i]==' ':         l.append(S[i+1]) c='' for i in l:     c+=i return c     
 
 
 
0
deeplakhotia8761 week ago
Simple Python solution
 
x="" s=S.split(" ") for i in s:     x+=i[0] return x 
 
Upvote if it helped you
0
mayank180919991 week ago
	string firstAlphabet(string S)
	{
	    // Your code goes here
	     string str;
	     str.push_back(S[0]);
	    for(int i=1;i<S.length();i++){
	        if(S[i]==' '){
	            str.push_back(S[i+1]);
	        }
	    }
	  return str;
	}
0
gurankitbehal781141 week ago
class Solution{public: string firstAlphabet(string S){    string s="";    s.push_back(S[0]);    for(int i=0;i<S.length();i++){       if(S[i]==' '){           s.push_back(S[i+1]);       }     }    return s;}};
0
manavkanjiyanijobupdate3 weeks ago
JAVA SOLUTION – ALL TEST CASE PASS
class Solution {   String firstAlphabet(String S) {       String s = "";        s = String.valueOf(S.charAt(0));       for(int i = 0; i < S.length()-1;i++)       {                               if(String.valueOf(S.charAt(i)).equals(" "))           {               s = s + S.charAt(i+1);                          }       }       return s;   }};
0
sanyasi13104 weeks ago
##JAVA#####
String[] ab = S.split(" ");       StringBuilder sb = new StringBuilder();       for(String str: ab){           sb.append(str.charAt(0));                  }       return sb.toString();
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.