sayeed_1999's blog

By sayeed_1999, history, 3 years ago, In English

Question:

You are given a sentence as a string. The sentence may have more than one space in between its words and also extra space at the beginning and end of the string. You have to print only the first letters of each word of the given sentence as a string.

Example test cases:

input:
abc def   ghi
output:
adg

input:
  abc   def z
output:
adz

Algorithm:-

string s; cin >> s; // take user sentence input

s.insert(0, ' '); // insert an extra space at the begining of string so that all the words surely have a space before it!

for(int i=1; s[i]; i++)
{
  if( s[i-1]==' ' && s[i]!=' ' ) cout << s[i];
}

tricky implementation, wasn't it?!

  • Vote: I like it
  • -20
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +23 Vote: I do not like it

Why not using while(cin>>s) then cout << s[0] ?