2

我是 c 编程语言的新手,我有一个与使用字符相关的大学教程作业(我不会为这个作业评分)你必须计算单词,我必须编译并在在线网络中提交我的答案我的代码将针对我不可见的测试用例运行的环境。这是我的任务:

编写函数 'wc',它返回一个包含如下格式的字符串: "NUMLINES NUMWORDS NUMCHARS NUMBYTES" 。空白字符是空格、制表符 (\t) 和换行符 (\n)。字符是任何不是空格的东西。给定的字符串以空字符 (\0) 结尾。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* wc(char* data) {
  char* result ;
  int numLine ;
  int numWords ;
  int numChars ;
  int i;
  int numBytes =strlen(data);
  char* empty=NULL;
  while(strstr(data,empty)>0){
    numWords=1;

    for (i = 0; i < sizeof(data); i++) {

    if(data[i]=='\n'){
     numLine++;
   }
    if(data[i]==' ' ){
     numWords++;
   }
    if(data[i]!=' '){
     numChars++;
   }
   }

   }

    sprintf(result, "%d %d %d %d", numLine, numWords, numChars, numBytes);
    return result;
}

这段代码会给我正确的输出结果,但我在这里遗漏了一些东西,至少测试告诉我。

4

5 回答 5

5

你有一个非常严重的错误:

  char* result;
  ...
  sprintf(result, "%d %d %d %d", numLine, numWords, numChars, numBytes);

这在 C 中是不允许的。您需要先为字符串分配足够的内存。声明result为一个足够大的静态数组,或者malloc如果您在课程中已经介绍过,请使用它。

例如

char buf[100];  // temporary buffer

sprintf(buf, "%d %d %d %d", numLine, numWords, numChars, numBytes);

char *result = malloc(strlen(buf) + 1);   // just enough for the string
strcpy(result, buf);                      // store the string

return result;
于 2013-03-19T17:53:23.607 回答
2

What if you have this input?

Two        Words.

You have to count the transitions between whitespace/non-whitespace, not just count spaces.


Also, I'm pretty sure strstr(data,NULL) will not do anything useful.

于 2013-03-19T17:49:20.500 回答
1

1) sizeof is wrong:

Instead of sizeof operator you need to use strlen() in for loop, like:

for (i = 0; i < strlen(data); i++)
                ^ not sizeof

sizeof(data) returns only size of data pointer address that is 4. Because you are to read all char in data[] you need strlen() that will return length of data[] (or number of chars in data[])

2) memory error:

Next Error I can notice there is no memory allocated for result. it declare like:

char* result ;

and No memory allocate! and you are writing using sprintf that cause undefined behavior of your code

3) while(strstr(data,empty)>0) is wrong

strstr() search position of a string in to other you empty string is NULL , CHECK: char *strstr(const char *s1, const char *s2);

you strstr() always returns data, Why are you calling this? I believe you don't need this while() loop.

I improved you code upto some extend as below, There was only three error as I mentioned above now corrected(to understand read comments), You basic algo is correct:

#define SIZE 256   // added size macro
char* wc(char* data)  
  char* result = malloc(SIZE*sizeof(char)); //(2) allocated memory for result 
  int numLine ;
  int numWords ;
  int numChars ;
  int i;
  int numBytes =strlen(data);
    numWords=1; 
                    // (3) remove while loop
    for (i = 0; i < strlen(data); i++) {  //(1) change size
        if(data[i]=='\n'){
         numLine++;
     }
        if(data[i]==' ' ){
         numWords++;
     }
        if(data[i]!=' '){
         numChars++;
     }
    }
    sprintf(result, "%d %d %d %d", numLine, numWords, numChars, numBytes);
    return result;
 }

int main(){
    printf("\nresult: %s\n", wc("q toei lxlckmc    \t \n ldklkjjls \n i \t nn "));
    return 1;
}

Output:

result: 2 14 28 41
于 2013-03-19T17:49:19.473 回答
1

wc unix命令源码:

http://www.gnu.org/software/cflow/manual/html_node/Source-of-wc-command.html

处理所有测试用例。

于 2013-03-19T17:57:46.493 回答
1

您似乎还缺少\t空格检查器中的 for 选项卡,并且您在输入或输出单词时没有正确检查。您可以为此使用在stdbool.hbool中定义的布尔类型。

于 2013-03-19T17:54:27.087 回答