An easy implementation problem and one single for loop solution! O(N)

Правка en3, от sayeed_1999, 2021-07-04 21:00:51

Question 1:

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?!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en5 Английский sayeed_1999 2021-07-04 21:03:10 8
en4 Английский sayeed_1999 2021-07-04 21:02:12 46
en3 Английский sayeed_1999 2021-07-04 21:00:51 22
en2 Английский sayeed_1999 2021-07-04 20:59:22 12
en1 Английский sayeed_1999 2021-07-04 20:58:02 751 Initial revision (published)