第五周作业

作业头

这个作业属于那个课程 C语言程序设计II
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/2824
我在这个课程的目标是 学会使用字符串函数
这个作业在那个具体方面帮助我实现目标 熟练运用数组,文件,并了解字符串函数
参考文献 c语言程序设计p166

7-1 英文单词排序 (25 分)

本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

输入格式:
输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

输出格式:
输出为排序后的结果,每个单词后面都额外输出一个空格。

输入样例:

blue
red
yellow
green
purple
#

输出样例:
red blue green yellow purple

1)实验代码

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

int main()
{
	FILE *fp = fopen("D:\myself\liqingxi\myfirst\five.txt","a+"); 
    char str[20][10],t[20];
    int i,j;
    int len;

    for(i=0;i<20;i++)
    {
        scanf("%s",str[i]);
        fputs(str[i],fp);
        fprintf(fp,"
");
        if(str[i][0]=='+')
            break;
    }
    
    len=i;  //单词的个数
    
    //选择排序,将单词排序(从小到大)
    for(i=0;i<len-1;i++)
    {
        for(j=1;j<len;j++)
        {
            if(strlen(str[j-1])>strlen(str[j]))
            {
                strcpy(t,str[j-1]);
                strcpy(str[j-1],str[j]);
                strcpy(str[j],t);
            }
        }
    }
    
    //输出
    for(i=0;i<len;i++)
    {
        printf("%s ",str[i]);
        fputs(str[i],fp);
        fprintf(fp," ");
    }
    
    fclose(fp);
    return 0;
}

2)设计思路

3)调试过程中遇到的问题和解决方案

4)运行结果截图

预习作业:

https://pintia.cn/problem-sets/1109223542200287232/problems/type/2

原文地址:https://www.cnblogs.com/xirfly/p/10585284.html