-1

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Ann
  • 53
  • 8
  • 3
    Why are you using `getline` if you need to get every word? `getline` gets every line. `cin >>` gets every word. – NathanOliver Aug 08 '19 at 18:13
  • 1
    Unrelated to your problem but please take some time to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Aug 08 '19 at 18:14
  • 1
    If you do need to gather input line by line, you can place the line in a `std::istringstream` and parse the `istringstream` with `>>` to get words. [See option 2 of this answer for an example.](https://stackoverflow.com/a/7868998/4581301) – user4581301 Aug 08 '19 at 18:15

2 Answers2

5

If you need to read multiple lines from the input, and treat them individually, then you could use an std::stringstream, like this:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(void)
{
  int lines_no;
  cin >> lines_no;
  // To ignore the trailing newline
  std::cin.ignore();

  while(lines_no--)
  {
    string line;
    // Read a line from the input
    getline(cin, line);

    // Construct a string stream based on the current line
    stringstream ss(line);

    string word;
    // For every word of the sstream,
    while(ss >> word)
      // print its first character
      cout << word[0];
    cout << endl;
  }
  return 0;
}

Input:

MY NAME IS ANKIT 
HELLO HOW ARE YOU

Output:

MNIA
HHAY

PS: I had to ignore the trailing newline character, as explained here.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • your method works fine but I need to take the integer input first for the number of different strings, your code works fine but it is printing a empty line first. how to remove that ? I know this is a silly question but I am not able to figure it out. help would be appreciated – Ann Aug 08 '19 at 18:51
  • @Ann what is the number representing? The number of lines to be read? Or the words of every line? As for the empty line, I cannot reproduce it, [run my code online](https://wandbox.org/permlink/1lDw0VxQD8Qd4nUA). You are welcome, my pleasure to help! :) – gsamaras Aug 08 '19 at 18:54
  • 1
    number is representing the number of lines and I have to print the first letter of every word for a single line and then a new line and do the same for the next line. – Ann Aug 08 '19 at 18:57
  • @Ann I now I see, thanks, check my updated answer, is that working? :) [Run it online](https://wandbox.org/permlink/R8Y5wTLxmGBnxGqa), if you want. – gsamaras Aug 08 '19 at 19:04
  • 1
    thanks. it's working fine. – Ann Aug 08 '19 at 19:08
2

As @NathanOliver commented, getline() reads every line, while std::cin reads every word, which is exactly what you need (If you are not convinced, read more in std::cin.getline( ) vs. std::cin).

Minimal example to get you started:

#include <iostream>
#include <string>

int main(void)
{
  std::string word;
  while(std::cin >> word) {
    std::cout << word[0] << "\n";
  }

  return 0;
} 

Output (for input: Antelope bird cat dog):

A
b
c
d

PS: As @SomeProgrammerDude mentioned: Why should I not #include <bits/stdc++.h>?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I need to get the output in next line suppose there are 2 test cases and the strings are MY NAME IS ANKIT and HELLO HOW ARE YOU so the output must be MNIA and then a new line and then HHAY. – Ann Aug 08 '19 at 18:22
  • @Ann then you need getline, which I described in my different answer [here](https://stackoverflow.com/a/57418740/2411320). PS: next time include the input and expected output in your question. :) – gsamaras Aug 08 '19 at 18:38
  • 1
    sure @gsamaras. – Ann Aug 08 '19 at 18:58