Here is a question of C++ that I was practising on some other site. The code is given below. My code fails for multiple inputs. the output is only clee olg
instead of clee olg sho col Given a string, S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as space-separated strings on a single line. INPUT- The first line contains an integer, t (the number of test cases). Each line 'i' of the subsequent lines contain a String,S OUTPUT- For each String S(where 0<= j<=N-1), print S's even-indexed characters, followed by a space, followed by S's odd-indexed characters. . Eg- 2 college school output-
clee olg sho col
int main()
{
int t;
cin>>t;
string str;
vector<string>s_even;
vector<string>s_odd;
for(int i=0;i<t;i++)
{
getline(cin,str);
for(int j=0;j<str.size();j++)
{
if(j%2==0)
{
string a;
a=str[j];
s_even.push_back(a);
}
else
{
string b;
b= str[j];
s_odd.push_back(b);
}
}
copy(s_even.begin(),s_even.end(),ostream_iterator<string>(cout));
cout<<" ";
copy(s_odd.begin(),s_odd.end(),ostream_iterator<string>(cout));
str.clear();
s_odd.clear();
s_even.clear();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire