2017-11-10 73 views

回答

0
m = n[i];// its wrong, 'm' is an integer and you are assigning char string. 

ň應該INT指針,因爲你是存儲長度進去。爲什麼這麼多的雙指針?你可以簡單地做。

檢查下面的修改。

#define SIZE 21 //defined index of the array 
int main(int argc,char* argv[]) 
{ 
     char * string[SIZE] = { "dfghgfd", "rtyukljgfds", "sdsf", "fdgdfhg", "fgfhgjghj", "nmjlkjlk", "qwasazx", 
       "zxdfd", "opiljkg", "vcxdfgfd", "fgfhgfhgh", "bvvh", "bb", "dfsdretr", 
       "reuio", "cvbmhg", "fgfdyrtyty", "fgdgdfgdfgdf", "g", "fgdfg", "ghghgfhv" }; 
     int k; 
     char *temp; 
     for (k = 0; k<SIZE - 1; k++) 
     { 
       for (j = k + 1; j<SIZE; j++) 
       { 
         if (strcmp(string[k],string[j])>0) 
         { 
           temp = string[k]; 
           string[k] = string[j]; 
           string[j] = temp; 
         } 
       } 
     } 
//  for (k = 0; k<SIZE; k++) 
//    printf("%s\n",string[k]); 
     return 0; 

} 
0

不確定爲什麼需要「a」數組,因爲您可以使用新的字符串數組交換字符。此外,使用char *來保存值的長度是奇怪的,但我想它的工作原理,因爲字符串的長度很短。

0

不知道您是否想對字母或單詞進行排序。評論部分對單詞進行排序。
檢查malloc的返回值,因爲它可能會失敗。

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

#define SIZE 21 //defined index of the array 

int main(int argc, char** argv) 
{ 
    // an array with 21 strings 
    char * string[SIZE] = { "dfghgfd", "rtyukljgfds", "sdsf", "fdgdfhg", "fgfhgjghj", "nmjlkjlk", "qwasazx", 
         "zxdfd", "opiljkg", "vcxdfgfd", "fgfhgfhgh", "bvvh", "bb", "dfsdretr", 
         "reuio", "cvbmhg", "fgfdyrtyty", "fgdgdfgdfgdf", "g", "fgdfg", "ghghgfhv" }; 

    int Anz, i; //Anz - 21 strings 
    int width = 0, len = 0; 

    //declared new array 
    char** new_string; 
    Anz = sizeof(string)/sizeof(*string); 
    if (NULL == (new_string = malloc (Anz * sizeof(*new_string)))) { 
     fprintf (stderr, "malloc failed\n"); 
     return 0; 
    } 

    for (i = 0; i < Anz; i++) 
    { 
     len = strlen (string[i]) + 1; 
     if (len > width) { 
      width = len;//used later when printing 
     } 
     if (NULL == (new_string [i] = malloc (width))) { 
      fprintf (stderr, "[i] malloc failed\n"); 
      //free memory allocated 
      while (i) { 
       i--; 
       free (new_string[i]); 
      } 
      free (new_string); 
      return 0; 
     } 
     strcpy(new_string[i], string[i]); 
    } 
/* 
    //sort words 
    int word = 0; 
    while (word < Anz - 1) { 
     int end = word; 
     int temp = end + 1; 
     while (end >= 0 && 0 > strcmp (new_string[temp], new_string[end])) { 
      char *hold = new_string[temp]; 
      new_string[temp] = new_string[end]; 
      new_string[end] = hold; 
      end--; 
      temp--; 
     } 
     word++; 
    } 
    word = 0; 
    while (word < Anz) { 
     printf ("Anz[%2d] is %s\n", word, new_string[word]); 
     word++; 
    } 
*/ 
    //sort letters in word 
    char swap; 
    int sorted; 
    int prior; 
    int each; 
    int start; 

    word = 0; 
    while (word < Anz) 
    { 
     start = 0;//new_string[Anz][0] 
     sorted = start; 
     prior = start; 
     each = start + 1;//new_string[Anz][1] 
     printf ("Anz[%2d] is %-*s", word, width, new_string[word]); 
     while ('\0' != new_string[word][each]) { 
      while (prior >= 0 && new_string[word][each] < new_string[word][prior]) { 
       swap = new_string[word][each]; 
       new_string[word][each] = new_string[word][prior]; 
       new_string[word][prior] = swap; 
       each--;//move toward start of string 
       prior--; 
      } 
      sorted++;//move toward end of string 
      prior = sorted; 
      each = prior + 1; 
     } 
     printf (" sorted %s\n", new_string[word]); 
     word++; 
    } 
    //release allocated memory 
    word = 0; 
    while (word < Anz) { 
     free (new_string[word]); 
     word++; 
    } 
    free (new_string); 

    return 0; 
}