簡體   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