2013-07-12 2 views
0

나는 지금 작가 블록을 가지고 있습니다.C - 배열의 char 문자열을 같은 char 사용자 입력으로 정렬합니다.

내가 원하는 것은 newWord가 wordInput과 같은지 확인하는 정렬을 사용하고, 그렇지 않으면 문자가 나타날 때까지 문자를 계속 스왑합니다. 예를 들어, wordInput이 poop이고 newWord가 oopp이라고 가정 해 봅시다. newWord가 결국에는 똥으로 변하기를 바랍니까, 어떻게하면 될까요?

이것은 내가 지금까지 가지고있는 코드입니다.

#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); 
} 
+4

'wordInput' 여전히 원래 문자열을 보유하고 있기 때문에, 왜 그냥 나에서'strcpy'를 사용하지? – simonc

+0

예, 물론입니다. – 0decimal0

+0

원본 입력으로 다시 정렬하는 데 도움을 얻은 후에는 각 스왑 후에 "newWord"가 무엇인지 표시하고 싶습니다. 예를 들면 다음과 같습니다. poop -> oopp -> opop -> poop – chakolatemilk

답변

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; 
} 
1

좋아, 기본적으로 정렬 된 문자 배열을 특정 (임의?) 순서로 변환하고 길을 따라 스왑을 기록하고 싶습니까?

여기에이 방법이 있습니다.

#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; 
     } 
    } 
    } 
} 
+0

내가 정말로 원하는 것은 프로그램이 실제 스와핑을 기술적으로 기록하지 않고 역방향으로 재생하는 것입니다. 내가 원하는 것은 기본적으로 다음과 같습니다. wordInput = poop, newWord = oopp, wordWrite와 같을 때까지 newWord가 문자를 계속 교환 할 수있게하려면 어떻게해야합니까? – chakolatemilk

+0

원래의 스왑을 기록하지 않습니다. "정렬 된 단어 % s을 (를) % s (으)로 다시 변환 ..."한 후 알고리즘을보십시오. 그것은 무엇을 wordInput (귀하의 경우에는 p)의 첫 번째 문자를 가지고, 그리고이 첫 번째 인스턴스를 newWord에서 찾습니다 (oopp의 첫 번째 p는 세 번째 문자 임). 그런 다음이 문자를 newWord의 현재 첫 문자로 바꿉니다. 그런 다음 wordInput의 다음 문자로 이동합니다. 귀하의 예제에서 스왑은 다음 oop -> 똥. 첫 번째 편지를 세 번째 편지와 교환하고 완료합니다. – Trenin