I want to print the first letter of every word in a string. I have used the getline function to get the string with spaces. It works fine for the a single test case, but not working for multiple test cases. kindly help why this is happening and if possible pose a solution to get the answer for multiple test cases.
#include<bits/stdc++.h>
using namespace std;
string firstLetterWord(string str) {
string result = "";
if(str[0]!=' ')result.push_back(str[0]);
for (int i=1; i<str.length(); i++) {
if (str[i] != ' ' && str[i-1] == ' ') {
result.push_back(str[i]);
}
}
return result;
}
int main() {
string str;
getline(cin,str);
cout << firstLetterWord(str);
return 0;
}
if I input 't' the number of test cases and then find the answers for the strings then the code is giving answer of only first test case.