Before going to his school's administrative office to submit his fixed records, Raphael had received an important call, and had written down a string str on the blackboard in a hurry. However, when he returned to his class, he noticed that the mischievous Gin has erased his important string str, and instead replaced it with a string str_new.
All is not lost, though. Raphael knows Gin's obfuscation tricks like the back of his hand. He knows from prior experience that Gin does the following to any string that he fancies: he takes its first letter, writes the second letter to the left of the first, then the third to the right of the new string, and so on. For example, under Gin's wrath the string "alberio" becomes "ielabro", and "betelguese" becomes "eegeebtlus".
Help Raphael get back his original string str from the
string that Gin has left on the blackboard!
Write a C++ program that takes as console input (cin) a string
str_new and prints out the original string str to
the console (cout)!
Sample Output:
Enter the scrambled string: cpsia
The original string was: spica
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str_new[50],original[50];
int i, k,j;
cout << "Enter a scrambled string : ";
cin>>str_new;
j = strlen(str_new) - 2;
for(i=0; j>=0;i++,j-=2)// to copy alternate characters from
end
original[i] = str_new[j];
for(k=0; k<=strlen(str_new)-1; k+=2)// to copy alternate
characters from beginning
original[i++]=str_new[k];
original[i]='';
cout << " original string : " << original;
return 0;
}
screenshot output: $g++ -o main *.cpp $main Enter a scrambled string : original string : egeebtlus
Before going to his school's administrative office to submit his fixed records, Raphael had received an...