问题描述
- 这段代码什么意思求解释
-
#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] = '';
}
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] != ''; j++ ) {
rwords[j] = words[count-j-1];
}
rwords[j] = '';
}/*
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
", k+1, words[k]);
}
}
解决方案
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.
输出结果是
- words.
- into
- broken
- be
- to
- line
- sample
- a
- is
- this
解决方案二:
关于这段奇怪代码的解释
解决方案三:
代码错误很多,我实在无聊,帮你改好了
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
//Constants
#define LINELENGTH 100
// Function prototypes
void reverse_words(char* words[], char* rwords[], int count);
void print_words(char* words[] , int count);
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 , count );
getchar();
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] = '';
}
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] != ''; j++ ) {
rwords[j] = words[count-j-1];
}
rwords[j] = '';
}
/*
Prints each word in words[] to stdout in order and preceded by its number.
*/
void print_words( char* words[] , int count)
{
int k;
for( k=0; k < count; k++ ) {
printf("%2d. %s
", k+1, words[k]);
}
}