5个字符串怎么编程序

时间:2025-01-29 00:39:08 网络游戏

将五个字符串写入文件中

```c

include

void main() {

FILE *fp;

char s;

if((fp=fopen("d:\\file5.txt","w"))==NULL) {

printf("can not open file !");

exit(0);

}

for(int i=0; i<5; i++) {

scanf("%s", s[i]);

fprintf(fp, "%s\n", s[i]);

}

fclose(fp);

}

```

从键盘输入五个字符串并找出最大的字符串

```c

include

include

int main() {

char s;

int i;

printf("输入五个字符串:\n");

for(i=0; i<5; i++) {

scanf("%s", s[i]);

}

char max;

strcpy(max, s);

for(i=1; i<5; i++) {

if(strcmp(max, s[i]) < 0) {

strcpy(max, s[i]);

}

}

printf("最大的字符串是:%s\n", max);

return 0;

}

```

从键盘输入五个字符串并按长度排序

```c

include

include

int main() {

char str;

int i, j;

char temp;

printf("请输入5个字符串:\n");

for(i=0; i<5; i++) {

scanf("%s", str[i]);

}

for(i=0; i<5; i++) {

for(j=i+1; j<5; j++) {

if(strlen(str[i]) > strlen(str[j])) {

strcpy(temp, str[i]);

strcpy(str[i], str[j]);

strcpy(str[j], temp);

}

}

}

printf("排序后的字符串:\n");

for(i=0; i<5; i++) {

printf("%s ", str[i]);

}

return 0;

}

```

从键盘输入五个字符串并按字典序排序

```c

include

include

void swap(char *x, char *y) {

char temp;

strcpy(temp, x);

strcpy(x, y);

strcpy(y, temp);

}

int main() {

char a;

int i, j;

printf("输入5个字符串:\n");

for(i = 0; i<5; i++) {

scanf("%s", a[i]);

}

for(i = 0; i<5-1; i++) {

for(j = 0; j<5-i-1; j++) {

if(strcmp(a[j], a[j+1]) > 0) {

swap(&a[j], &a[j+1]);

}

}

}

printf("排序后的字符串:\n");

for(i = 0; i<5; i++) {

puts(a[i]);

}

return 0;

}

```

使用冒泡排序对五个字符串按字典序排序