2015-02-10 456 views
0

我通过搜索位和找不到与没有使用字符串库的功能,如STRCMP字符的比较,所以希望这个问题不重复。我来自Java,对C语言有点新鲜。我试图比较两个字符串(保存在同一个数组中)以按字母顺序对它们进行排序,但试图在没有C字符串库函数的情况下这样做,并且似乎无法弄清楚这一点。任何人都可以将我指向正确的方向吗?如何比较两个字符串中的字符以按字母顺序排序? (无C串库函数)

for (i = 0; i < TotalStrings; i++) { /* TotalStrings is the number of strings in the array */ 
if (length[i] < length[i+1]) { /* compares length of both strings, saved in a different array */ 
for (j = 0; j < length[i]; j++) { 
    if (Strings[i][j] > Strings[i+1][j]) { 
     char temp = Strings[i]; 
     Strings[i] = Strings[i+1]; 
     Strings[i+1] = temp; 
     j = length[i]; 
    } 
    } 
} 
if (length[i] > length[i+1]) { /* compares length of both strings, saved in a different array */ 
for (j = 0; j < length[i+1]; j++) { 
    if (Strings[i][j] > Strings[i+1][j]) { 
     char temp = Strings[i]; 
     Strings[i] = Strings[i+1]; 
     Strings[i+1] = temp; 
     j = length[i]; 
     } 
     } 
    } 

行if(Strings [i] [j]> Strings [i + 1] [j])是卡住的地方。正如我已经教过的,二维数组中的第一个支架包含字符串,第二个支架指向字符?我不确定如何去比较这些字符串的字符。很确定,试图做到这一点,因为我(像我会数字)关闭。

另外,不知道这是否与我上面的代码有关(由于它未完成或因为temp是char类型而Strings是char类型的二维数组;但是我得到一个指向equals的错误登录下面的语句:

error: incompatible types when assigning to type 'char[1000]' from type 'char' 
      Strings[i+1] = temp; 

编辑:在我的代码现在看,我可以看到一个巨大的缺陷,它会在字符串相同指数在运行到一个字[X]进来后性格字符串[X + 1],然后更改字符串一起;这是不对的,我会改变我的代码来纠正,但我仍然不知道如果我比较字符以正确的方式

+0

是否允许*非字符串*函数?如'qsort()'。如果他们是,那可能会让你更容易(虽然授予,'qsort()'可能会令人困惑)。 – 2015-02-10 01:54:27

+0

另外,我们可以看看你如何声明'字符串? – 2015-02-10 01:57:26

+0

@TimČas我将Strings声明为'Char Strings [TotalStrings] [MaxLength]'。非字符串函数没有问题,但我正试图学习如何编写一个循环来检查每个单独的字符(如果有必要) – newJavaUser 2015-02-10 02:02:03

回答

0

修订(见下文)
关于错误:
看起来
char temp = Strings[i];
应该是:
char *temp = Strings[i];
我不知道为什么,当你分配一个编译器没有错误字符变量与指针,但这是看起来是问题。 我建议在C中阅读pointers,特别是如果你来自Java。

关于您的原始问题:
看起来您正在比较正确的方式。
另外,是的,正如你已经教过的,二维数组中的第一个括号包含字符串,第二个括号指向字符。

UPDATE:
正如乔纳森说,温度实际上应该是:
char temp[1000];
但因为你没有使用标准库函数,你需要手动复制字符串。

+0

实际上,它在我看来更像'temp'应该是'char temp [1000];'而赋值应该是字符串复制操作。 – 2015-02-10 04:30:25

+0

更新后,谢谢@JonathanLeffler,你是对的。 – 2015-02-10 16:21:49

1

由于@Nunzio Tocci已经显示在你的代码的问题。

在你的代码的问题是, 指定字符指针为char变量,反之亦然。

您可以通过将函数分解为函数来改善代码,比如一个用于比较字符串,另一个用于对它进行排序,因为您也不能使用strcpy(因为您说没有标准库调用),所以可以编写函数字符串复制,并实现代码。

下面是一个例子,你可以看看它并从这里开始。

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

// Compare two strings, returns -1, 0 ,1 
int string_compare(const char* s1, const char* s2) { 
    // perfrom null dereferencing error checking 
    while((*s1==*s2) && *s1) 
     s1++,s2++; 
    return *s1 < *s2 ? -1 : *s1 > *s2; 
} 

// Copy s2 into s1 
int string_copy(char *s1, const char* s2){ 
    // perfrom null dereferencing error checking 
    while((*s1++ = *s2++)) 
     ; 
    return 0; 
} 

// Sort the array of string 
char *const *const string_sort(char *const *const str, const int  totalString,const int strMaxLen) { 

    char *temp = malloc(strMaxLen); // Using temp for swapping 
    int i,j; 

    for (i = 0; i < totalString; i++) { 
     for (j = 0; j < totalString - 1; j++) { 
      if (string_compare(str[j], str[j + 1]) > 0) { 
       string_copy(temp,str[j]); 
       string_copy(str[j],str[j + 1]); 
       string_copy(str[j + 1],temp); 
      } 
     } 
    } 
    free(temp); 
    return str; 
} 

int main() { 
    /* No of strings/ can be known at runtime also 
     since we are not using array */ 
    const int totalString = 10; 
    const int strLen = 1000; // string length 

    // Pointer to store address of an the array of string. 
    char **str = NULL; 
    int i; 

    /* Allocate memory for storing the 
     address of "totalString" no of stirngs */ 
    str = malloc(totalString*sizeof(char*)); 

    /* allocate memory to store a string 
     witb 'strLen' length */ 
    for(i = 0; i < totalString; ++i) 
     str[i] = malloc(strLen); 

    /* Read from the user or 
     any external source (ex: file) */ 
    for(i = 0; i < totalString; ++i) 
     scanf("%s",str[i]); 

    /* sort the strings */ 
    string_sort(str,totalString,strLen); 

    /* print then */ 
    for(i = 0; i < totalString; ++i) 
     printf("%s\n",str[i]); 

    for(i = 0; i < totalString; ++i) 
     free(str[i]); 
    free(str); 
    return 0; 
} 
+0

你能解释一下原代码中的缺陷是什么,你的代码有什么不同,以及它为什么会起作用?在没有任何评论的情况下转储一段代码并不是很有帮助。 – nwellnhof 2015-02-10 10:10:10

+0

注意:'while&(* s1 && * s2 &&(* s1 == * s2)'''不需要'&& * s2''。建议首先放置'* s1 == * s2':'while((* s1 == * s2)&& * s1)'。 – chux 2015-02-10 18:57:05

+0

@chux我同意不需要'&& * s2',但'(* s1 == * s2)'的顺​​序并没有什么区别。 – Sridhar 2015-02-11 07:33:17

相关问题