书后练习--字符串数组操作(综合练习1-16 到 1-19)

书中给的例题: 

#include <stdio.h>
#define MAXLINE 1000        /* 输入的最大长度 */

int getline( char line[], int maxline );
void copy( char to[], char form[] );

void main()
{
    int len;                /* 当前行长度 */
    int max;                /* 目前为止发现的最长行的长度 */
    char line[MAXLINE];        /* 当前的输入行 */
    char longest[MAXLINE];    /* 用户保存最长的行 */

    max = 0;

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

/* 将一行读入s中并返回其长度 */
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] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
    
}

/* 将from复制到to; 这里假设to足够大 */
void copy( char to[], char from[] )
{
    int i;

    i = 0;
    while ( (to[i] = from[i]) != '\0' )
    {
        ++i;
    }
}

 

练习1-16  修改打印最长文本行的程序主程序main,使之可以打印任何长度的输入行的长度

View Code
#include <stdio.h>
#define MAXLINE 1000        /* 输入的最大长度 */

int getline( char line[], int maxline );
void copy( char to[], char form[] );

void main()
{
    int len;                /* 当前行长度 */
    int max;                /* 目前为止发现的最长行的长度 */
    char line[MAXLINE];        /* 当前的输入行 */
    char longest[MAXLINE];    /* 用户保存最长的行 */

    max = 0;

    while ( (len=getline(line, MAXLINE)) > 1 )
    {
        copy ( longest, line );
        printf("长度:%d,输出:%s", len, longest);
    }
    
}

/* 将一行读入s中并返回其长度 */
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] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
    
}

/* 将from复制到to; 这里假设to足够大 */
void copy( char to[], char from[] )
{
    int i;

    i = 0;
    while ( (to[i] = from[i]) != '\0' )
    {
        ++i;
    }
}

 

结果:

 

练习1-17  编写一个程序,打印长度大于80个字符的所有输入行

View Code
#include <stdio.h>
#define MAXLINE 1000        /* 输入的最大长度 */

int getline( char line[], int maxline );
void copy( char to[], char form[] );

void main()
{
    int len;                /* 当前行长度 */
    int max;                /* 目前为止发现的最长行的长度 */
    char line[MAXLINE];        /* 当前的输入行 */
    char longest[MAXLINE];    /* 用户保存最长的行 */

    max = 0;

    while ( (len=getline(line, MAXLINE)) > 1 )
    {
        if ( len > 80 )
        {
            copy ( longest, line );
            printf("长度:%d,输出:%s", len, longest);
        }
    }
    
}

/* 将一行读入s中并返回其长度 */
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] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
    
}

/* 将from复制到to; 这里假设to足够大 */
void copy( char to[], char from[] )
{
    int i;

    i = 0;
    while ( (to[i] = from[i]) != '\0' )
    {
        ++i;
    }
}

结果:

 

练习 1-18 删除每个输入行末尾的空格及制表符,并删除完全是空格的行。

View Code
#include <stdio.h>
#define MAXLINE 1000        /* 输入的最大长度 */

int getline( char line[], int maxline );
int copy( char to[], char form[], int length );

void main()
{
    int len;                /* 当前行长度 */
    int to_len;                /* 新字符串当前行长度 */
    int max;            /* 目前为止发现的最长行的长度 */
    char line[MAXLINE];        /* 当前的输入行 */
    char longest[MAXLINE];    /* 用户保存最长的行 */

    max = 0;

    while ( (len=getline(line, MAXLINE)) )
    {
        to_len = copy ( longest, line, len );
        if ( to_len > 0 ) {
            printf("原始长度:%d,编译后长度:%d,%s", len, to_len, longest);
        } 
        else 
        {
            printf("原始长度:%d,编译后长度:%d\n", len, to_len);
        }
    }
    
}

/* 将一行读入s中并返回其长度 */
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] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
    
}

/**
* 将from复制到to; 这里假设to足够大, length 字符串长度 
* 并取出空格,制表符 返回新字符串长度
*/
int copy( char to[], char from[], int length )
{
    int i = 0, j = 0;
    length = length - 2;
    while ( from[length] == ' ' || from[length] == '\t' ) 
    {
        length--;
    }
    
    if ( length >= 0 ) {
        for ( i=0; i<=length; i++ ) 
        {
            to[i] = from[i];
            j++;
        }
        to[i] = '\n';
        to[++i] = '\0';
        j += 1;
    }    
    return j;
}

结果:

 

 练习 1-19  编写函数,颠倒一个输入行中的字符串

View Code
#include <stdio.h>
#define MAXLINE 1000

int getline( char s[], int line );
void  reverse ( char s[], int line );

void main ()
{
    int len, to_len;                /* 当前行数 */
    char s[MAXLINE];                /* 当前输入行 */
    char to[MAXLINE];                /* 倒叙输入的行 */

    while ( (len = getline(s, MAXLINE)) > 0 ) 
    {        
        reverse(s, to, len);
        printf("%s", to);
    }
    
}

/* 将一行读入form中并返回长度 */

int getline ( char s[], int line )
{
    int c, i;

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

/* 将字符串颠倒输出 */

void reverse ( char s[], char to[], int line )
{
    int i, j = 0;

    for ( i=line-1; i>=0; i--)
    {
        to[j] = s[i];
        j++;
    }
    to[j] = '\n';
    to[++j] = '\0';
}

结果:

posted @ 2012-11-04 21:08  无本之木a  阅读(297)  评论(0编辑  收藏  举报