0

I am having writer's block at the moment.

What I want is to have a sort that will check if newWord equals to wordInput, and if it doesn't, it will keep swapping around letters until it does. For example, let's say wordInput is poop and newWord is oopp, I want newWord to eventually turn into poop, so how can I swap that around?

This is the code I have so far.

#include<stdio.h>
#include<string.h>

int main(){
    char wordInput[25];
    char newWord[25];
    char tmp;
    int len;
    int a, b;

    wordInput = "poop";
    newWord = "oopp";

    len = strlen( newWord );

    // Sort back to wordInput
    for( a = 1; a < len; a++ ){
        for( b = 0; b < len - a; b++ ){
            if( newWord[b] != wordInput[b] ){
                tmp = newWord[b];
                newWord[b] = newWord[b + 1];
                newWord[b + 1 ] = tmp;
            }
        }
    }
    printf( "Back to original input: %s\n", newWord );
}
chakolatemilk
  • 833
  • 9
  • 21
  • 31
  • 4
    Since `wordInput` still holds the original string, why not just use that or `strcpy` from it? – simonc Jul 12 '13 at 12:47
  • After I get the help to sort it back to original input, I want it to show what the "newWord" is after each swap. For example: poop -> oopp -> opop -> poop – chakolatemilk Jul 12 '13 at 12:50
  • 1
    There is no way to undo the sort unless you record each swap you make. For example, you could keep an array of swaps and then undo them by going through them backwards. The reason why you can't undo a sort is that sorting applies some ordering to an unordered list. In order to revert this, you'd need to know the nature of the original unordered list in order to undo it all. – Trenin Jul 12 '13 at 12:52
  • Okay, how about this? Forget the fact that I already did a swap in general. Let's say wordInput = poop and newWord = oopp, how can I make newWord keep swapping until it equals to wordInput? – chakolatemilk Jul 12 '13 at 15:11

2 Answers2

1

OK, so basically you want to convert a sorted array of letters to a specific (random?) ordering and record the swaps along the way, right?

Here is one way to do this.

#define SWAP(a,b) a^=b;b^=a;a^=b

int main(int argc, char* argv[]) {
  char* wordInput=argv[1];
  char* newWord = (char*)malloc((strlen(wordInput) + 1) * (sizeof(char)));

  int i,j,k;
  fprintf(stdout, "Word is %s\n", wordInput);
  // Sort wordInput into newWord.
  for (i=0; i<strlen(wordInput); i++) {
    // Put this one at the end.
    newWord[i]=wordInput[i];
    // Start at the back of the string, and move it forward if it is less.
    for (j=i-1; j>=0; j--) {
      if (newWord[j+1] < newWord[j]) {
      SWAP(newWord[j+1], newWord[j]);
      } else {
        break;
      }
    }
  }
  newWord[strlen(wordInput)]='\0';

  fprintf(stdout, "Converting sorted word %s back to %s...\n", newWord, wordInput);
  // Recover the original word making swaps.
  for (i=0; i<strlen(wordInput)-1; i++) {
    // Locate this letter in the newWord.
    for (j=i; j<strlen(newWord); j++) {
      if (newWord[j]==wordInput[i]) {
        // Move this letter to the front if it isn't already there.
        if (i != j) {
          SWAP(newWord[j], newWord[i]);
          fprintf(stdout, "Swapping %d with %d --> %s\n", i, j, newWord);
        }
        break;
      }
    }
  } 
}
Trenin
  • 2,041
  • 1
  • 14
  • 20
  • What I really want the program to do is not technically record the actual swapping and then play it backwards. What I want it to do is basically this: wordInput = poop, newWord = oopp, how can I make newWord keep swapping out letters until it equals to wordInput? – chakolatemilk Jul 12 '13 at 15:09
  • This doesn't record the original swaps. Look at the algorithm after "Converting sorted word %s back to %s...". What it does it take the first letter in wordInput (p in your case), and locates the first instance of this in newWord (first p in oopp is the third letter). It then swaps this letter with the current first letter in newWord. Then it goes to the next letter in wordInput and so on. In your example, the swaps would then be oopp -> poop. It would swap the first letter with the third and be done. – Trenin Jul 23 '13 at 17:59
1
#include <stdio.h>
#include <string.h>

void swap(char *a, char *b){
    char wk = *a;
    *a = *b;
    *b = wk;
}

int main(void){
    char wordInput[25];
    char newWord[25];
    int i, len;
    char *p;

    strcpy(wordInput, "poop");
    strcpy(newWord, "oopp");

    len = strlen( newWord );//assert(strlen(newWord)==strlen(wordInput))

    printf("newWold:%s\n",newWord);
    for(i = 0; i < len; ++i ){
        if(wordInput[i] == newWord[i])
            continue;
        if(NULL!=(p=strchr(&newWord[i+1], wordInput[i])))
            swap(&newWord[i], p);
        else
            break;
    }
    if(i < len){
        printf("can't...orz\n");
    } else {
        printf( "Back to original input: %s\n", newWord );
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70