繁体   English   中英

在c编程中使用Char

[英]Working with Char in c programming

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

编写函数'wc',它返回一个包含格式如下的字符串:“NUMLINES NUMWORDS NUMCHARS NUMBYTES”。 空白字符是空格,制表符(\\ t)和新行(\\ n)。 角色是任何不是空白的东西。 给定的字符串为null-char(\\ 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;
}

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

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

  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;

如果你有这个输入怎么办?

Two        Words.

您必须计算空白/非空白之间的转换,而不仅仅是计数空间。


另外,我很确定strstr(data,NULL)不会做任何有用的事情。

1) sizeof错误:

而不是sizeof运算符,你需要在for循环中使用strlen() ,如:

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

sizeof(data)仅返回数据指针地址的大小为4 因为你读的所有字符data[]您需要strlen()将返回的长度data[]或数字符的data[]

2) 内存错误:

下一个错误我可以注意到没有为result分配内存。 它声明如下:

char* result ;

没有内存分配! 并且您正在使用sprintf编写,导致代码的未定义行为

3) while(strstr(data,empty)> 0)错误

strstr()在其他空字符串中搜索字符串的位置为NULL ,CHECK: char * strstr(const char * s1,const char * s2);

你strstr()总是返回data ,为什么要调用它? 我相信你不需要这个while()循环。

我改进了你的代码,如下所示,只有三个错误,正如我上面提到的现在纠正( 了解阅读评论 ),你的基本算法是正确的:

#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;
}

输出:

result: 2 14 28 41

您似乎也缺少白色空格检查器中的\\t标签,并且您没有正确检查何时进入或离开单词。 您可以在stdbool.h中为此定义boolean类型bool

wc unix命令的源代码:

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

处理所有测试用例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM