我是c编程语言的新手,我有一个与使用字符相关的大学教程作业(我不会为这个作业评分),在那里你必须计算单词,我必须编译和在线网络环境提交我的答案在我的代码将运行对测试用例是不可见的me.here是我的任务:在c编程中使用char
Write the function 'wc' which returns a string containing formatted as follows: "NUMLINES NUMWORDS NUMCHARS NUMBYTES" . Whitespace characters are blanks, tabs (\t) and new lines (\n). A character is anything that is not whitespace. The given string is null-char (\0) terminated.
这里是我的代码:
#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;
}
这代码会给我正确的输出结果,但我在这里错过了一些东西至少测试告诉我这一点。
如果你确实得到正确的结果,那么你是什么意思“我错过了什么”? – 2013-03-19 17:48:45
他不知道正在使用的确切测试用例。 – 2013-03-19 17:50:01