【问题标题】:sort words alphabeticaly in C在C中按字母顺序对单词进行排序
【发布时间】:2016-02-08 13:40:35
【问题描述】:

我有一个问题,我的 C 语言程序必须找到有 N 个字母的单词,计算它们并按字典顺序对它们进行排序,然后将它们保存到另一个文件中。如何按字母顺序对单词进行排序?

这是我的代码:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stddef.h>
#include <string.h>

int main()
{
FILE *r, *fp;
char ch[100],t[100];
int n,i,j,x=0;


r=fopen("text.txt","r");
fp=fopen("text2.txt","w");
fgets(ch, 100, r);


char *start;
int len;
char *s = ch;
char *p = s;
printf("Give the length of word: ");
scanf("%d",&n);
printf("\n\nWords with %d letters: \n\n",n);
    while (*p) {

        while (*p && isspace(*p))
            ++p;
        start = p; 

        while (*p && !isspace(*p))
            ++p;

        len = p - start;

        if (len == n) {

            printf("%.*s\n", len, start);
              x++;

            fprintf(fp,"%.*s",len, start);

        }    
    }      


printf("\nNumber of words: %d ",x);
fclose(fp);



getch();      
fclose(r);  
} 

【问题讨论】:

  • 实现自己的排序功能

标签: c sorting


【解决方案1】:

有一个用于排序的标准库函数:

https://en.wikipedia.org/wiki/Qsort

【讨论】:

    【解决方案2】:

    编写一个按字母顺序对单词进行排序的函数很容易,它与对字母字符进行排序非常相似。要对字符进行排序,您遍历数组并一次比较两个字符。如果第一个大于第二个,则交换它们并再次循环直到完成。如果是单词,您必须遍历字符串,并遍历它们的字符以进行必要的交换。这是一个示例:

    #include <stdio.h>
    
    int s_bubblesort(int argc,char **argv);
    
    int main(void)
    {
        char *elements[9] = { "zab","aaaa","bac","zzz","abc","cab","aaa","acc","aaaaaaaaa" };
    
        s_bubblesort(9,elements);
    
        for( int n = 0 ; n < 9 ; n++ )
        {
            printf("%s\n",elements[n]);
        }
    }
    
    int s_bubblesort(int argc,char **argv)
    {
        //loop variables
        int i,j;
    
        //p_1 : pointer that points to current string,p_2 : pointer that points to next string
        char *p_1 , *p_2 , *tmp;
    
        for( j = 0 ; j < argc ; j++ )
        {
            for( i = 0 ; i < argc - j - 1 ; i++ )
            {
                //assign p_1 to current string,and p_2 to next string
                p_1 = argv[i] , p_2 = argv[i+1];
    
                //while *p_1 != '\0' and *p_2 != '\0'
                while( *p_1 && *p_2 )
                {
                    //if *p_1 less than *p_2,break and move to next iteration
                    if( *p_1 < *p_2 )
                        break;
    
                    else if( *p_1 > *p_2 || ( ! *(p_2 + 1) && ( *p_1 == *p_2 ) && *(p_1+1) ) ) // if *p_1 > *p_2,or *(p_2 + 1 ) == '\0' and *p_1 == *p_2 and *(p_1 + 1 ) != '\0' { SWAP }
                    {
                        tmp = argv[i];
    
                        argv[i] = argv[i+1];
    
                        argv[i+1] = tmp;
    
                        break;
                    }
                    p_1++;
                    p_2++;
                }
            }
        }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2015-12-08
      • 1970-01-01
      • 2018-11-20
      相关资源
      最近更新 更多