Thread: Rearranging a string... and a glitch

  1. #16

    I contest that this code is more optimized!

    [indignant]
    harumph ()
    [/indignant]

    I finally got my code to work. Just uncomment out the lines that are commented to add back in the functionality that i don't have, if they are needed.

    Code:
    #include <iostream>
    #include <conio.h>  // getche();
    
    int main(int argc, char *argv[])
    {
      char sInput[1024];
    
      int iLength = 0;
      int i = 0, x = 0, y = 0, z = 0;
    
      cout << "What message do you want to scramble?\n";
      cin.get(sInput, 1024);
      //cin.ignore(1024, '\n');  // Do you really need this?
    
      iLength = strlen(sInput);
      int half = iLength/2;
      //strrev(sInput);            // I don't have this function, sorry.
    
      char *sOutput = new char[iLength];
      for(int p=0;p<iLength;p++)sOutput[p]='-';
      sOutput[iLength]='\0';
      for (i=0, x=0; (i<iLength) && (sInput[x] != '\0'); i++, x=i/2){
        if (i%2==0){
          sOutput[half+x]=sInput[i];
        } else {
          sOutput[x]=sInput[i];
        }
      }
      cout << "Scrambled:   " << sOutput << endl;
      for (i=0, x=0; (i<iLength) && (sInput[x] != '\0'); i++, x=i/2){
        if (i%2==0){
          sInput[i]=sOutput[half+x];
        } else {
          sInput[i]=sOutput[x];
        }
      }
      cout << "Descrambled: " << sInput << endl;
    
      if (getche());
      return 0;
    }
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #17
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    wow. lol

  3. #18
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed