My goal is to: Given a string S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string.h>
using namespace std;
int main() {
int test;
cin>>test;
for(int j = 0; j < test; j++){
char str[10000];
cin.ignore();
cin.getline(str,9999);
//cin>>str;
for( int i = 0; i < strlen(str); i++)
{
if(i % 2 == 0)
cout<<str[i];
}
cout <<" ";
for(int i = 0; i < strlen(str); i++)
{
if((i % 2 != 0))
cout<<str[i];
}
cout << endl;
}
return 0;
}
The above code gives me output as :
Hce akr
ak n
for the given input of :
2
Hacker
Rank
But when I use cin>>str instead of the cin.ignore and cin.getline I get the correct expected output
Hce akr
Rn ak
Aucun commentaire:
Enregistrer un commentaire