2017-04-30 3 views
0

Dies ist ein Programm, das ich geschrieben habe, das die einzelnen Zeichen der Wörter in alphabetischer Reihenfolge mit einer Auswahl Sortiermethode sortieren soll.Auswahl Sortierung Char C

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

char str[100]; 

void alphaOrder(){ 

for(int i=0;str[i]!='\0';++i){         //iterate until end of entered string 
    if(str[i+1]==' ' || str[i+1]==NULL){  // iterate until end of current word  

      int wordLength=0;             // finds amount of letters in current word  
      for(int j=i;j>=0 && str[j]!=' ';j--){      
      wordLength+=1; 
      } 
      int smallest=1000;     
      int prv = 1000; 

      for(int k=0;k != wordLength;++k){ 
       int counter=0;          //loops through letters in word printing and storing the smallest 
       while(counter!=wordLength){  
        if(k==0){             // this if loop only occurs during for 1st char of word 
         if(str[i-counter] < smallest){ 
         smallest=str[i-counter]; 
         } 
        } 

        else{ 
         if(str[i-counter] > smallest && str[i-counter] < prv){ 
          smallest=str[i-counter]; 
          prv=smallest; 
         } 
        } 
       ++counter; 
       } 
       printf("%c",smallest);  
      }   
    } 
} 

Aber die Frage, die ich bin stammt in diesem Ausschnitt aus der Logik mit:

for(int k=0;k != wordLength;++k){ 
       int counter=0;          //loops through letters in word printing and storing the smallest 
       while(counter!=wordLength){  
        if(k==0){             // this if loop only occurs during for 1st char of word 
         if(str[i-counter] < smallest){ 
         smallest=str[i-counter]; 
         } 
        } 

        else{ 
         if(str[i-counter] > smallest && str[i-counter] < prv){ 
          smallest=str[i-counter]; 
          prv=smallest; 
         } 
        } 
       ++counter; 
       } 
       printf("%c",smallest); 

zum Beispiel:

input: hello world 
output:eoooo ddddd 

Ich weiß, dass das Problem von der Logik stammt, aber es ist war eine Woche und ich kann immer noch nicht richtig den Auswahl Sortieralgorithmus implementieren oder herausfinden, was genau ich brauche. so sind irgendwelche Ratschläge oder Anregungen stark

begrüßt
+0

Was haben Sie dies zu debuggen getan? Was hast du gedruckt? Was verfolgen Sie in Ihrem Debugger? Welchen Wert hat 'wordLength' bei der Verarbeitung von' world'? Warum verwenden Sie eine globale Variable in der Funktion? Sie sollten die zu sortierende Zeichenfolge an die Funktion übergeben. AFAICS, du versuchst zuerst "Hallo", dann "Hallo", dann "Llo" usw. zu sortieren. Aber du sortierst die Zeichenfolge nicht; Du druckst Charaktere aus, während du gehst und sie nicht neu anordnest. Solltest du nicht die Daten neu anordnen? Sie sollten die Funktion in mindestens zwei teilen - eins zum Teilen, eins zum Sortieren. –

Antwort

0

wie diese

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

void alphaOrder(char *str){ 
    char *p = str; 
    while(*p){ 
     while(isspace((unsigned char)*p)) 
      ++p;//skip space 
     if(!*p)//end of string? 
      break; 
     char *word = p;//top of word 
     while(*p && !isspace((unsigned char)*p)) 
      ++p;//in word 
     //selection sort 
     int word_length = p - word; 
     for(int i = 0; i < word_length -1; ++i){ 
      int smallest = i; 
      for(int j = i + 1; j < word_length; ++j){ 
       if(word[smallest] > word[j]) 
        smallest = j; 
      } 
      if(i != smallest){ 
       char temp = word[i]; 
       word[i] = word[smallest]; 
       word[smallest] = temp; 
      } 
     } 
    } 
} 
int main(void){ 
    char str[100+1]; 
    fgets(str, sizeof str, stdin); 
    alphaOrder(str); 
    puts(str); 
} 
+0

Wenn das _Wort_ nur aus Alphabeten besteht, verwenden Sie "isalpha" anstelle von "isspace". – BLUEPIXY