若干个字符串排序,然后写到文件中

来源:互联网 发布:java并发从入门到精通 编辑:程序博客网 时间:2024/06/15 22:32

#define_CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define LINE  3

int main()

{

         charstr[LINE][10]={0};

         inti = 0 ;

         chartemp[10] = {0} ;

         intm = 0 , n = 0 ;

         FILE*fp ;

 

         ////////输入多个字符串,存放在二维数组中,每个一维数组存放一个字符串

         printf("请输入 %d 个字符串:\n",LINE);

         for(i=0; i < LINE ;i++ )

                   gets(str[i]);

 

         ///////选择排序法对二维数组进行排序

         printf("把二维数组进行排序:\n");

         for(m = 0 ; m  < LINE-1 ; m++ )

                   for(n = m+1 ; n  < LINE ; n++ )

                            if((strcmp(str[m],str[n]))> 0 )

                            {

                                     strcpy(temp , str[m] );

                                     strcpy(str[m] , str[n] );

                                     strcpy(str[n], temp );

                            }

 

                            //////把排序好的字符数组存到文件中

                            printf("把二维数组存写到文件中,其结果为:\n");

                            if((fp= fopen ( "str.txt","w")) ==NULL )

                            {

                                     fprintf(stderr , "can't open the \"str.txt\" file.\n");

                                     exit(0);

                            }

 

                            for(i = 0 ; i < LINE ;i++ )

 

                            {

                                     fputs(str[i] , fp);

                                     fputs("\n" , fp);

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

                            }

 

                            return0 ;

}

0 0
原创粉丝点击