【C语言】简单片段输入多行

∥☆過路亽.° 提交于 2020-02-23 13:40:43

第一行输入数字:n表示 将输入n行句子

后续每行都输入句子

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define  SLEN   128

int getStrings(char * str)
{
    int num = 0;
    char c;

    do{
        scanf("%c", &c);
        str[ num ++ ] = c;
    }while(c != '\n' && num < SLEN);

    str[num - 1] = '\0';

    return num - 1;
}


int main(int argc, char ** argv)
{
    int    i = 0, j = 0;
    int    num = 0;
    char  **pStr;
    char   tmp[SLEN] ;
    char   cEnt;

    scanf("%d", &num); //第一行输入num行句子

    scanf("%c", &cEnt); //回车键

    //分配存储   num行句子的二维数组
    pStr = (char **)malloc(num*sizeof(char *));
    for(i = 0; i < num ; i++)
    {
        pStr[i] = (char *)malloc(SLEN*sizeof(char));
        memset(pStr[i], 0, SLEN);
        getStrings(pStr[i]);
    }

    //输出输入的每一行
    for(i = 0; i < num ; i++)
    {
        printf("%s\n", pStr[i]);
    }
    return  0;
}

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!