whitespace-这段代码什么意思求解释

问题描述

这段代码什么意思求解释

#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.

输出结果是

  1. words.
  2. into
  3. broken
  4. be
  5. to
  6. line
  7. sample
  8. a
  9. is
    1. 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]);
    }
}

时间: 2024-10-29 15:46:06

whitespace-这段代码什么意思求解释的相关文章

孙鑫-Visual C++的一段代码搞不懂求解释

问题描述 Visual C++的一段代码搞不懂求解释 说一下自己的理解.题目是孙鑫老是的文本输出的例子. 1:GetBkColor得到背景白色,然后SetTextColor(白色)把背景白色设置为文字颜色.跟着clr应该是赋值得到白色了吧,但是F5看的时候是黑色,就要原文字颜色.我这么理解应该错了 1 COLORREF clr=dc.SetTextColor(dc.GetBkColor()); //GetBkColor属于dc对象,改变dc中的背景值(白色).然后SetTextColor用白色值

printf-为什么这一小段代码总是运行时错误 求大家帮帮忙

问题描述 为什么这一小段代码总是运行时错误 求大家帮帮忙 #include<stdio.h>#include<string.h>int main(){ int t; scanf(""%d""&t); while(t--) { int n; scanf(""%d""&n); while(n--) { char a[]="">+""; char

java代码执行顺序求解释?

问题描述 java代码执行顺序求解释? public class ExA { private static ExA a = new ExA(); static { System.out.println("父类--静态代码块"); } public ExA() { System.out.println("父类--构造函数"); } { System.out.println("父类--非静态代码块"); } public static void ma

刚接触IO流有一段代码不理解求大神给我说明一下

问题描述 刚接触IO流有一段代码不理解求大神给我说明一下 红色框框标记的是不明白的. 再次表示感谢! 解决方案 Java 下 IO 中Reder 和 InputStream 分别是以字符和字节的形式来完成数据的读取的,然而返回值确是 int 类型的数据,这样做的核心目的只是要取到到一个 int 类型下的 -1 来表示数据流的末尾. 此次使用的read(b,0,512)是将文件中的数据读取到字节缓冲区b中,并返回读取到的字节的总数.循环读取文件内容到缓冲区,并写入另一个文件中.循环处理直到到达读取

c# 代码 注释-C# 求解释 两句话问好处

问题描述 C# 求解释 两句话问好处 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace 汉诺塔递归 { public partial class F

ancy orm-用FancyForm添加用户角色,求解释这段代码,

问题描述 用FancyForm添加用户角色,求解释这段代码, var form = $('#form').FancyForm({ title: '添加角色' width: 345 height: 325 inputWidth: 190 labelWidth: 60 url: '<%=path %>/sysRoleController/addRole.do' params: { param1: 1 param2:'string' } defaults: { type: 'string' } it

ip地址-求解释下这段代码...感激不尽

问题描述 求解释下这段代码...感激不尽 u_long ulDestIP; u_long ulDestIP1=inet_addr(a); u_long ulDestIP2=inet_addr(b); unsigned long count = ntohl(ulDestIP2 - ulDestIP1)-1; struct in_addr in; unsigned long hostip; for(unsigned int i = 0; i <=count+1; i++) { hostip = nt

java web-这段代码看不懂啊,求解释

问题描述 这段代码看不懂啊,求解释 /s:iterator /s:iterator/sx:treenode/s:iterator </sx:treenode> </s:iterator> </sx:treenode> </s:iterator> 解决方案 <sx:tree label="%{#request.project.projectName}" id="parentId" > //项目名 <s:

堆栈-一段奇怪的代码,求解释

问题描述 一段奇怪的代码,求解释 代码的意思是说,在声明的时候,因为栈的分配原因(先入后出),k[10]就是i的地址.然后i就会被修改成0,循环再次开始,就这样出现了一个死循环.为什么k[10]就是i的地址呢? int m=1; int i =0; int k[10] = {0}; printf(""&m=%dn""&m); printf(""&i=%dn""&i); printf("