Question

Before going to his school's administrative office to submit his fixed records, Raphael had received an...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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
Add a comment
Know the answer?
Add Answer to:
Before going to his school's administrative office to submit his fixed records, Raphael had received an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT