再读《The C Programming Language》 第一章 1.9 字符数组

1.9 字符数组

描述需求:读取一堆文本行,然后把最长的那句打印出来。
技术实现:书中写的非常好,用大白话把程序实现的思路一一描述了出来。我就不啰嗦了!当然这时候,如果是读谭老师的书,应该教同学们画流程图了!这边书似乎没有教大家画流程图,流程图还是非常非常的有用!找个时间,我要好好学学。
帖程序:
#include <stdio.h>      /* 系统头文件 */
#include <stdlib.h>     /* 系统头文件 */

#define MAXLINE        1000      /* lower limit of temperature scale */

//  函数声明
int getline( char s[], int lim );
void copy( char from[], char to[] );


/* Print the longest input line */
int main()
{
    int len;                /* currunt line length */
    int max;                /* maximum length seen so far */
    char line[MAXLINE];     /* currunt input line */
    char longest[MAXLINE];  /* longest line saved here */

    max = 0;
    while ( ( len = getline( line, MAXLINE )) > 0 )
        if ( len > max )
        {
            max = len;
            copy( line, longest );
        }
    if ( max > 0 ) /* there was a line */
        printf( "%s", longest );

    system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
    return 0;
}

/* read a line into s, return length */
int getline( char s[], int lim )
{
    int c, i;

    for ( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
        s[i] = c;

    if( c == '\n' )
    {
        s[i] = '\n';
        ++i;
    }
    s[i] = '\0';

    return i;
}

/* copy: copy 'from' to 'to'; assume to is big enough */
void copy( char from[], char to[] )
{
    int i = 0;
    while( (to[i] = from[i]) != '\0')
        ++i;
}

Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text. 
解析:打印任意输入行的长度,这样理解对吧?
#include <stdio.h>      /* 系统头文件 */
#include <stdlib.h>     /* 系统头文件 */

#define MAXLINE        10000      /* lower limit of temperature scale */

//  函数声明
int getline( char s[], int lim );
void copy( char from[], char to[] );


/* Print the longest input line */
int main()
{
    int len;                /* currunt line length */
    int max;                /* maximum length seen so far */
    char line[MAXLINE];     /* currunt input line */
    char longest[MAXLINE];  /* longest line saved here */
    int nline;
    int nlen[MAXLINE] = {0};    /* Every line's length saved here */

    max = 0;
    nline = 0;
    while ( ( len = getline( line, MAXLINE )) > 0 )
    {
        if ( len > max )
        {
            max = len;
            copy( line, longest );
        }

        // added code
        nlen[nline] = len;
        ++nline;            //  start from 1
       
    }
       
    if ( max > 0 ) /* there was a line */
        printf( "%s", longest );

    while(nline > 0)
    {
        printf(" %4d line length is : %6d\n", nline, nlen[--nline] );
    }

    system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
    return 0;
}

/* read a line into s, return length */
int getline( char s[], int lim )
{
    int c, i;

    for ( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
        s[i] = c;

    if( c == '\n' )
    {
        s[i] = '\n';
        ++i;
    }
    s[i] = '\0';

    return i;
}

/* copy: copy 'from' to 'to'; assume to is big enough */
void copy( char from[], char to[] )
{
    int i = 0;
    while( (to[i] = from[i]) != '\0')
        ++i;
}



Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
解析:我怎么知道会输入多少个80个字符以上的行呢?总不能建立N个longest[]的数组来保存80个以上字符吧,所以只要检测到输入行超过80个字符就把其打印出来,这个就简单了。

#include <stdio.h>      /* 系统头文件 */
#include <stdlib.h>     /* 系统头文件 */

#define MAXLINE         10000      /* lower limit of temperature scale */
#define MINLENS         80

//  函数声明
int getline( char s[], int lim );
void copy( char from[], char to[] );


/* Print the longest input line */
int main()
{
    int len;                /* currunt line length */
    char line[MAXLINE];     /* currunt input line */
    char longest[MAXLINE];  /* longest line saved here */


    while ( ( len = getline( line, MAXLINE )) > 0 )
    {
        if ( len > MINLENS )
        {
            copy( line, longest );
            printf("%s",longest );
        }
    }
       
    system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
    return 0;
}

/* read a line into s, return length */
int getline( char s[], int lim )
{
    int c, i;

    for ( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
        s[i] = c;

    if( c == '\n' )
    {
        s[i] = '\n';
        ++i;
    }
    s[i] = '\0';

    return i;
}

/* copy: copy 'from' to 'to'; assume to is big enough */
void copy( char from[], char to[] )
{
    int i = 0;
    while( (to[i] = from[i]) != '\0')
        ++i;
}

Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. 
解析:这个功能很实用,很多编辑器都有移除行尾空白和去空白行的功能;

#include <stdio.h>      /* 系统头文件 */
#include <stdlib.h>     /* 系统头文件 */

#define MAXLINE         10000      /* lower limit of temperature scale */
#define MINLENS         1

//  函数声明
int getline( char s[], int lim );
void copy( char from[], char to[] );


/* Print the longest input line */
int main()
{
    int len;                /* currunt line length */
    char line[MAXLINE];     /* currunt input line */
    char longest[MAXLINE];  /* longest line saved here */


    while ( ( len = getline( line, MAXLINE )) > 0 )
    {
        if ( len > MINLENS )
        {
            for( ; line[len-2] == ' ' || line[len-2] == '\t'; --len )
                ;
           
            line[len+1] = '\n';
            line[len+2] = '\0';
            copy( line, longest );
            printf("%s",longest );
           
        }
    }
       
    system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
    return 0;
}

/* read a line into s, return length */
int getline( char s[], int lim )
{
    int c, i;

    for ( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
        s[i] = c;

    if( c == '\n' )
    {
        s[i] = '\n';
        ++i;
    }
    s[i] = '\0';

    return i;
}

/* copy: copy 'from' to 'to'; assume to is big enough */
void copy( char from[], char to[] )
{
    int i = 0;
    while( (to[i] = from[i]) != '\0')
        ++i;
}

Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time. 
解析:反转,这个比较简单,似乎要用到一个算法,比如除去'\n' 有偶数个字符,举例len = 7,即有6个可用字符,即s[0],s[1],s[2],s[3],s[4],s[5];把s[0],和s[5]互换,换几次呢?(len-1)/2 = 3,就可以了, 如果len = 8,有7个字符,(len-1)/3 = 3;还是等于3 !这个第一节温度转换的例子已经解释过了哈。 s[0],s[1],s[2],s[3],s[4],s[5],s[7]; s[0]和s[7]互换,s[3]就不动啦!贴代码了。

#include <stdio.h>      /* 系统头文件 */
#include <stdlib.h>     /* 系统头文件 */

#define MAXLINE         10000      /* lower limit of temperature scale */
#define MINLENS         1

//  函数声明
int getline( char s[], int lim );


/* Print the longest input line */
int main()
{
    int len;                /* currunt line length */
    char line[MAXLINE];     /* currunt input line */ 
    int i, temp;


    while ( ( len = getline( line, MAXLINE )) > 0 )
    {
        if ( len > MINLENS )
        {
           for (i = 0; i < (len - 1) / 2; i++)
           {
                temp             = line[i];
                line[i]          = line[len - 1 - i];
                line[len -1 - i] = temp;
           }
           printf("%s", line);
            
        }
    }
        
    system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
    return 0;
}

/* read a line into s, return length */
int getline( char s[], int lim )
{
    int c, i;

    for ( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
        s[i] = c;

    if( c == '\n' )
    {
        s[i] = '\n';
        ++i;
    }
    s[i] = '\0';

    return i;
}

这节完了,知道什么是字符数组吗?呵呵,似乎不知道。但知道了如何去操作数组,如何去用,如何去处理;这就是国内和国外写书的区别,国内大部分写书,不管三七二十一,先把概念定义(所谓的规矩)给摆下!一副权威的样子~我们似乎要走的路还很长~
【本节完】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值