GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

how to read string with space in c++ for multiple test case?

void fun(string); int main() { int t;cin>>t; while(t--){ string b; (cin,b); fun(b);} return 0; } void fun(string b) { cout<<b<<endl; }

asked Sep 20, 2016 by sushil jaiswal

cant we use gets here ???

 

2 Answers

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char string[100];
    int t;
    scanf("%d",&t);
    fflush(stdin);
    while(t--)
    {
        scanf ("%[^\n]%*c", string);
        printf ("%s\n", string);
    }
return 0;
}

 

answered Sep 22, 2016 by Mazhar MIK
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string name;
  int t;
  cin>>t;
  while(t>=0){
  getline (cin,name);
  cout << name << "\n";
  t--;
    }
  return 0;
}

 

answered Sep 23, 2016 by intakhab Ali
...