码迷,mamicode.com
首页 > 其他好文 > 详细

字符串输入的拓展

时间:2016-06-15 20:52:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const size_t BUFFER_LEN =128;
const size_t NUM_P =3;
char buffer[128];
char *pS[3]={NULL};
char *pbuffer =buffer ;
int i;
int main()
{
    printf("\n You can enter up to %u message each up to %u ge ",NUM_P,BUFFER_LEN-1);
    
    for (i=0;i<NUM_P;i++)
    {
        pbuffer=buffer ;
        printf("\n  Enter %s message ",i>0?"another":"a");
        
        while ((pbuffer-buffer<BUFFER_LEN-1)&&(*(pbuffer++)=getchar())!=\n);
        
        if((pbuffer-buffer)<2)
            break;
        
        if((pbuffer-buffer)==BUFFER_LEN&&*(pbuffer-1)!=\n)
        {
            printf("String too long ");
            i--;
            continue;
            
        }
        
        
        
        *(pbuffer-1)=\0;
        
        pS[i]=(char*)malloc(pbuffer-buffer);
        
        if(pS[i]==NULL)
        {
            printf("\n OUT of memory ");
            return 1;            
        }
        
        strcpy(pS[i],buffer);    
        
    }
    
    printf("\n In reverse order .the strings you entered are :\n");
    
    while (--i >= 0)
    {
        printf("\n%s",pS[i]);
        free(pS[i]);
        pS[i]=NULL;
    }
    
    system("pause");
    return 0;
    
    
    
    
}

 

字符串输入的拓展

标签:

原文地址:http://www.cnblogs.com/xinqidian/p/5588778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!