Eclipse_____ 2023-12-08 02:30 采纳率: 62.5%
浏览 28
已结题

C语言字符串不区分大小写字典排序相关问题

原题:输入 10 个字符串,然后排序输出。排序的原则由键盘输入的数来决定,若为 0,则将 输入的字符串按整数值大小由小到大排序,否则按字典顺序
-排序(不区分大小写。要求:输入、输出、排序 分别用函数实现,主函数只是调用这些函数。

代码

  1. 问题:在第二种不区分大小写排序中,最后几个字符串无法实现排序。

代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void input(char (*p)[30], int n, int *p1);
void output(char (*p)[30], int n);
void sort1(char(*p)[30], int n);
void sort2(char(*p)[30], int n);

void main()
{
    int value=0;
    char str[10][30]; 
    printf("please input the value,determining the method of string sorting.\n");
    scanf("%d",&value);
    input(str,10,&value);
    output(str,10); 
    
}

void input(char(*p)[30],int n,int *p1)
{   int i;

    printf("please input %d character strings\n",n);
    
    for(i=0;i<n;i++)
    {
        malloc(sizeof(char) * 30);
        scanf("%s",p[i]);
    }
    if(*p1==0)
    {
        sort1(p,10);
    }
    else
    {
        sort2(p,10);
    }
}

void sort1(char(*p)[30], int n)
{
    int i,j;
    
    for(i=0;i<n-1;i++) 
    {
        for(j=i+1;j<n;j++)
        {
            if(strcmp(p[i],p[j])>0)
            {
                char * temp;
                strcpy(temp,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],temp);
            }
        }
        
    }
}

void sort2(char(*p)[30],int n)
{
    int i,j;
    char copy[10][30];
    for(i=0;i<n;i++)
    {
        malloc(sizeof(char) * 30);
        strcpy(copy[i],p[i]);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<30;j++)
        {
            if(copy[i][j]>='a'&&copy[i][j]<='z')
            {
                copy[i][j]-=32;
            }
        }
    }
    
    for(i=0;i<n-1;i++) 
    {
        for(j=i+1;j<n;j++)
        {
            
            if(strcmp(copy[i],copy[j])>0)
            {
                char temp[30];
                strcpy(temp,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],temp);
            }
        }
        
    }

}

void output(char (*p)[30], int n)
{   int i;
    printf("now showcase the sorted strings.\n");
        for(i=0;i<n;i++)
    {
        printf("%s\n",p[i]);
    }
}


  • 写回答

14条回答 默认 最新

  • micthis 2023-12-08 05:49
    关注

    主要错误有两个:
    1
    sort1中的temp应定义成数组而不是指针
    2
    sort2中应同时交换copy数组的元素

    改好了的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void input(char (*p)[30], int n, int *p1);
    void output(char (*p)[30], int n);
    void sort1(char(*p)[30], int n);
    void sort2(char(*p)[30], int n);
    int main()
    {
        int value=0;
        char str[10][30]; 
        printf("please input the value,determining the method of string sorting.\n");
        scanf("%d",&value);
        input(str,10,&value);
        output(str,10);
        return 0;
    }
    void input(char(*p)[30],int n,int *p1)
    {   int i;
        printf("please input %d character strings\n",n);
        for(i=0;i<n;i++)
        {
            //malloc(sizeof(char) * 30);
            scanf("%s",p[i]);
        }
        if(*p1==0)
        {
            sort1(p,10);
        }
        else
        {
            sort2(p,10);
        }
    }
    void sort1(char(*p)[30], int n)
    {
        int i,j;
        for(i=0;i<n-1;i++) 
        {
            for(j=i+1;j<n;j++)
            {
                if(strcmp(p[i],p[j])>0)
                {
                    char temp[30];
                    //char * temp;
                    strcpy(temp,p[i]);
                    strcpy(p[i],p[j]);
                    strcpy(p[j],temp);
                }
            }
        }
    }
    void sort2(char(*p)[30],int n)
    {
        int i,j;
        char copy[10][30];
        for(i=0;i<n;i++)
        {
            //malloc(sizeof(char) * 30);
            strcpy(copy[i],p[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;copy[i][j]!='\0';j++)
            //for(j=0;j<30;j++)
            {
                if(copy[i][j]>='a'&&copy[i][j]<='z')
                {
                    copy[i][j]-=32;
                }
            }
        }
        for(i=0;i<n-1;i++) 
        {
            for(j=i+1;j<n;j++)
            {
                if(strcmp(copy[i],copy[j])>0)
                {
                    char temp[30];
                    strcpy(temp,copy[i]);
                    strcpy(copy[i],copy[j]);
                    strcpy(copy[j],temp);
                    strcpy(temp,p[i]);
                    strcpy(p[i],p[j]);
                    strcpy(p[j],temp);
                }
            }
        }
    }
    void output(char (*p)[30], int n)
    {   int i;
        printf("now showcase the sorted strings.\n");
            for(i=0;i<n;i++)
        {
            printf("%s\n",p[i]);
        }
    }
    

    运行结果截图:

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 创建了问题 12月8日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵