Skip to content

Instantly share code, notes, and snippets.

@sanikamal
Created September 21, 2022 16:27
Show Gist options
  • Save sanikamal/49544d2a9e817a70de882f5bbfe149a9 to your computer and use it in GitHub Desktop.
Save sanikamal/49544d2a9e817a70de882f5bbfe149a9 to your computer and use it in GitHub Desktop.
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
<?php
$handle = fopen ("php://stdin","r");
$t = fgets($handle);
for($i = 0; $i < $t; $i++) {
$input = trim(fgets($handle));
$even = '';
$odd = '';
for($j = 0; $j < strlen($input); $j++) {
if(($j & 1) == 0) {
$even .= $input[$j];
}
else {
$odd .= $input[$j];
}
}
printf("%s %s\n", $even, $odd);
}
fclose($handle);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment