使用指针对字符串排序

 

 

#include <stdio.h>
#include <stdlib.h>
typedef enum {false=0,true=1}bool;
#include <string.h>
#define BUFFER_LEN  100
#define NUM_P 5
char buffer[BUFFER_LEN];
char *pS[NUM_P]={NULL};
char *pTemp =NULL;
int i=0;
bool sorted=false ;
int last_string =0;

int main(void)
{
    printf("\n Enter successive lines,pressing Enter at the \n\n\n\n ");
    while ((*fgets(buffer,BUFFER_LEN,stdin)!='\n')&&(i<NUM_P))
    {
        pS[i]=(char*)malloc(strlen(buffer)+1);
        if(pS[i]==NULL)
        {
            printf("Memory allocation failed.Program terminated.\n");
            return 1;
        }
        strcpy(pS[i++],buffer);
        
    }
    last_string=i;
    
    while(!sorted)
    {
        sorted=true;
        for(i=0;i<last_string-1;i++)
        {
                if(strcmp(pS[i],pS[i+1])>0)
                {
                    sorted=false;
                    pTemp=pS[i];
                    pS[i]=pS[i+1];
                    pS[i+1]=pTemp;
                }
        }
        
    }
    printf("\n You input sorted in order is:\n\n");
    for(i=0;i<last_string;i++)
    {
        printf("%s\n",pS[i]);
        free(pS[i]);
        pS[i]=NULL;
    }
    system("pause");
    
    return 0;
}

 

 

 

fgets()函数用于从文件流中读取一行或指定个数的字符,其原型为:
    char * fgets(char * string, int size, FILE * stream);

参数说明:
string为一个字符数组,用来保存读取到的字符。
size为要读取的字符的个数。如果该行字符数大于size-1,则读到 size-1 个字符时结束,并在最后补充' \0';如果该行字符数小于等于 size-1,则读取所有字符,并在最后补充 '\0'。即,每次最多读取 size-1 个字符。
stream为文件流指针。

【返回值】读取成功,返回读取到的字符串,即string;失败或读到文件结尾返回NULL。因此我们不能直接通过fgets()的返回值来判断函数是否是出错而终止的,应该借助feof()函数或者ferror()函数来判断。

注意:fgets()与gets()不一样,不仅仅是因为gets()函数只有一个参数 FILE *stream,更重要的是,fgets()可以指定最大读取的字符串的个数,杜绝了gets()使用不当造成缓存溢出的问题。

 

 

 

#include <stdio.h>
#include <stdlib.h>
typedef enum {false=0,true=1}bool;
#include <string.h>
#define BUFFER_LEN  10
#define NUM_P 5
char buffer[BUFFER_LEN];
char *pS[NUM_P]={NULL};
char *pTemp =NULL;
int i=0;
bool sorted=false ;
int last_string =0;

int main(void)
{
    printf("\n Enter successive lines,pressing Enter at the \n ");

    while ((*fgets(buffer,BUFFER_LEN,stdin)!='\n')&&(i<NUM_P))//只要小于字符串长度,只要没有按下'\n',不断读取
    {
        pS[i]=(char*)malloc(strlen(buffer)+1);//开内存,个数需要加一,因为要存'\0'
        
        if(pS[i]==NULL)
        {
            printf("Memory allocation failed.Program terminated.\n");
            return 1;
        }
        strcpy(pS[i++],buffer);//复制到新内存     
    }

    last_string=i;//记录最后一个字符串的索引??
    
    while(!sorted)//冒泡排序,这里仅仅交换指针,内存还是在各自内存里
    {
        sorted=true;
        for(i=0;i<last_string-1;i++)
        {                                                                                                                                                                                                                                                                                             
                if(strcmp(pS[i],pS[i+1])>0)
                {
                    
                    pTemp=pS[i];
                    pS[i]=pS[i+1];
                    pS[i+1]=pTemp;
                    sorted=false;//作为交换完成标志,反正目的就是strcmp(pS[i],pS[i+1])<0
                }
        }
        
    }

    printf("\n You input sorted in order is:\n\n");
    for(i=0;i<last_string;i++)
    {
        printf("%s\n",pS[i]);
        free(pS[i]);
        pS[i]=NULL;
    }
    system("pause");
    
    return 0;
}

 

posted @ 2016-06-16 13:46  V5八旗  阅读(1077)  评论(0编辑  收藏  举报