Frankjunyu 2015-10-14 05:16 采纳率: 58.8%
浏览 1649
已采纳

这段代码什么意思求解释

#include
#include
#include
#include

//Constants
#define LINELENGTH 100

// Function prototypes
void reverse_words(char* words[], char* rwords[], int count);
void print_words(char* words[]);
int mark_words(char* line, char* words[]);

// Main Function
int main( void )
{
char *line;
char *words[51];
char *rwords[51];
int count;

if(( line = (char*) malloc(LINELENGTH * sizeof(char))) == NULL ) {
return 1;
}
strcpy(line, "this is a sample line to be broken into words.");

count = mark_words( line, words );

reverse_words( words, rwords, count );

print_words( rwords );

return 0;
}

/*
Converts line to a packed string of words and puts
pointers to each word in words[].

returns the number of words it found.
/
int mark_words( char
line, char* words[] )
{
int i, count = 1;
char inbetween = 0;

words[0] = line;
for( i=0; line[i] != 0 ; ++i ) {
if( isspace( line[i] )) {
inbetween = 1;
line[i] = '\0';
}
else { // i.e. line[i] is not a whitespace character
if( inbetween ) {
inbetween = 0;
words[count++] = line + i;
}
}
}
words[count] = NULL;
return count;
}

/*
Copies the pointers in words[] to rwords[] in reverse order.
count is the number of pointers in words[]
/
void reverse_words( char
words[], char* rwords[], int count )
{
int j = 0;

for( j = 0; words[j] != '\0'; j++ ) {
rwords[j] = words[count-j-1];
}
rwords[j] = '\0';
}

/*
Prints each word in words[] to stdout in order and preceded by its number.
/
void print_words( char
words[] )
{
int k;

for( k=0; words[k] != 1; k++ ) {
printf("%2d. %s\n", k+1, words[k]);
}
}

  • 写回答

2条回答 默认 最新

  • toplinq 2015-10-14 05:48
    关注

    int mark_words(char* line, char* words[]);
    主要的用途是,把一行的单词按空格分开,每个单词的字符串指针放到words数组里面,并返回单词个数

    void print_words(char* words[]);
    打印words数组中每一个单词

    void reverse_words(char* words[], char* rwords[], int count);
    把words数组中的单词按逆序反过来,放到rwords数组中

    测试是
    this is a sample line to be broken into words.

    输出结果是

    1. words.
    2. into
    3. broken
    4. be
    5. to
    6. line
    7. sample
    8. a
    9. is
      1. this
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧