需要malloc输入:结构体中定义的word是指针类型的
一定要malloc,否则输出颜色字符串的时候它只让你输出一个
函数题也可以加头文件strlen------include<string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
struct wInfor
{
char *word;//每个单词最大长度为10
int l;
} ;
void input(struct wInfor w[MAXSIZE] ,int n);
void wordSort(struct wInfor w[MAXSIZE] ,int n);
int main()
{
int n,i;
struct wInfor word[MAXSIZE];
scanf("%d",&n);
getchar();
input(word,n);
wordSort(word,n);
for(i=0;i<n;i++)
printf("%s ",word[i].word );
return 0;
}
答案:
/* 请在这里填写答案 */
#include<string.h>
void input(struct wInfor w[MAXSIZE] ,int n)
{
int i;
for(i=0;i<n;i++)
{
w[i].word=(char *)malloc(sizeof(char));
scanf("%s",w[i].word);
w[i].l=strlen(w[i].word);
}
}
void wordSort(struct wInfor w[MAXSIZE] ,int n)
{
int i,j;
struct wInfor c;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(w[j].l>w[j+1].l)
{
c=w[j];
w[j]=w[j+1];
w[j+1]=c;
}
}
}
}