c - 按字母顺序排列二维数组

标签 c string multidimensional-array openvms

我正在用 C 语言做一个字符串练习,我必须对文本中的一些单词进行排序。

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

main(){
        int cch=0, cw=0, i, j, w=0, ord=0, f=0; //counter and index
        char testo[80];
        char alfa[50][25];
        char swap[25];

        printf("Write the test:\n");
        gets(testo);

        if(testo[0]!='\0'){
                cw=1;   
                for(i=0;testo[i]!='\0';i++){
                        cch++;
                        if(testo[i]==' '){
                                cw++;
                        }
                }
        }

        for(i=0;i<cch;i++){
                if(testo[i]==' ' && testo[i+1]==' '){
                        cw--;
                }
        }

        if(testo[0]==' '){
                cw--;
                w--;
        }

        printf("\nIn the test there are %d characters\n", cch);
        printf("In the test there are %d words\n", cw);

        if(cw>0){
                printf("\nUsed words:\n");
                for(j=0;j<cch;j++){
                        if(testo[j]==' ' && testo[j+1]==' '){
                                //nothing to do       
                        }
                        else{
                                if(testo[j]!=' '){
                                        alfa[w][f]=testo[j];
                                        f++;
                                }
                                else if(testo[j]=='\0'){
                                        alfa[w][f]='\0';
                                        f=0;
                                        w=0;
                                }
                                else{
                                        alfa[w][f]='\0';
                                        w++;
                                        f=0;
                                }
                        }
                }

                for(i=0;i<cw;i++){
                        printf("%d> %s\n", i+1, &alfa[i]);
                }

                //order
                f=1;
                printf("\nWord used in alphabetical order:\n");
                while(f==1){
                        f=0;
                        for(i=0;i<cw-1;i++){
                                ord=strcmp(alfa[i],alfa[i+1]);
                                if(ord>-1){
                                        strcpy(swap,alfa[i]);
                                        strcpy(alfa[i],alfa[i+1]);
                                        strcpy(alfa[i+1],swap);
                                        f=1;
                                }       
                        }
                }

                for(i=0;i<cw;i++){
                        printf("%d> %s\n", i+1, alfa[i]);
                }
        }
        else{
                printf("You haven't written any word.\n");
        }
}

问题是,如果有两个相同的词,而且词数大于2,我循环了,没有结果,怎么办? 在 OpenVMS 上测试。 谢谢。

PS:我知道目前有很多错误,但我无法解决这个问题。

最佳答案

if(ord>-1){
    /* ... */
} 

如果两个词相同,strcmp 将返回 0。这将交换这两个词,直到你的下一个电费账单进来并且你关闭你的程序。相反,检查结果是否大于零:

if(ord > 0){
    /* ... */
}

另请参阅:strcmp

关于c - 按字母顺序排列二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320452/

相关文章:

c - 如何让字符设备驱动程序内核模块的 write 调用者进入休眠状态

c - 需要帮助转换 C Mersenne twister 实现以对我有用

java - Pattern.compile() 抛出异常

arrays - MATLAB:将多维数组中的子数组删除为一个数组

javascript - 根据匹配字符串过滤多维数组

c - Bison:纯推送解析器中存储的最后一个 $$ 值在哪里?

c - 添加数组元素并存储在数组中

string - 按字面意思将字符串转换为字节

ruby - Ruby 中的正则表达式负后视似乎不起作用

c - 多维数组到图像