C和指针学习笔记: 第一章:快速上手

第一章:快速上手

程序描述:
//我们所要分析的这个程序从标准输入读取文本并对其进行修改,然后把它写到标准输出。程序1.1首先读取一串列标号。这些列标号成对出现,表示输入行的列范围。这串列标号以一个负值结尾,作为结束标志。剩余的输入行被程序读入并打印,然后输入行中被选中范围的字符串被提取出来并打印。注意,每行第1列的列标号为零。

//每个输入行的后面一行是该行内容的一部分.
//出输入的第1行是一串列标号,串的最后以一个负数结尾.
//这些列标号成对出现,说明需要打印的输入行的列的范围.
//例如,0 3 10 12 -1表示第0列到第3列,第10列到第12列的内容将被打印.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS  20
#define MAX_INPUT 1000

int read_column_numbers( int columns[], int max);
void rearrange( char *output, char *input, int n_colums, int const conlumns[]);

int main() {

    int n_columns;
    int columns[MAX_COLS];
    int input[MAX_INPUT];
    int output[MAX_INPUT];

    n_columns = read_column_numbers( columns, MAX_COLS);
    while(gets(input) != NULL)
    {
        printf("Original input:%s\n",input);
        rearrange(output, input, n_columns, columns);
        printf("Rearranged line:%s\n",output);
    }

    return 0;
}

int read_column_numbers( int columns[], int max)
{
    int num = 0;
    int ch;

    while(num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0)
        num += 1;

    if (num % 2 != 0)
    {
        puts("Last column number is not paried");
        exit(0);
    }

    while((ch = getchar()) != EOF && ch != '\n')
        ;

    return num;
}

void rearrange( char *output, char *input, int n_colums, int const columns[])
{
    int col;
    int output_col;
    int len;

    /*该参数用于检查是否超出输入字符长度*/
    len = strlen(input);

    /*output作为字符串头指针,output_col作为字符串长度,相加则可得到字符串尾指针*/
    output_col = 0;

    for( col = 0; col < n_colums; col += 2)
    {
        /*从这到后续两个if是为了确定尾部新添加的字符串的长度*/
        int nchars = columns[col + 1] - columns[col] + 1;

        if (nchars >= len || output_col == MAX_INPUT - 1)
            break;

        if (output_col + nchars > MAX_INPUT - 1)
            nchars = MAX_INPUT - output_col -1;

        /*从尾部添加字符串节选,并更新得到的新字符串长度*/
        strncpy(output + output_col, input + columns[col], nchars);
        output_col += nchars;
    }

    /*添加字符串结束标志*/
    output[output_col] = '\0';
}

函数使用笔记:

  • gets(char *) : 读取一行; 会主动丢弃换行符,并在该行的末尾存储一个NUL字节’; 读取成功gets函数返回一个非NULL值,否则返回NULL

  • getchar() : 读取单个字符

  • scanf(): 成功则返回1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ColaForced

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值