Rearranging a string... and a glitch

This is a discussion on Rearranging a string... and a glitch within the C++ Programming forums, part of the General Programming Boards category; [indignant] harumph ( ) [/indignant] I finally got my code to work. Just uncomment out the lines that are commented ...

  1. #16
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    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
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21