所以我試圖從getline函數中獲取字數,但是我一直在收到分段錯誤錯誤。在這裏,你可以假定空白只會被定義爲'\ t','\ n'和''。如何從getline做一個字數?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int tokenCount(char *mystring){
int word=0;
char *ptr = mystring;
int i;
for(i=0; i<strlen(mystring);i++){
if(ptr[i]!=' ' || ptr[i]!= '\t' || ptr[i]!='\n'){
word++;
while(ptr[i]!= ' ' || ptr[i]!= '\t' || ptr[i] != '\n'){
i++;
}
}
}
return word;
}
int main(){
size_t n = 10;
char *mystring = malloc(10);
if(mystring==NULL){
fprintf(stderr, "No memory\n");
exit(1);
}
while(getline(&mystring, &n, stdin)>0){
printf("%d\n", tokenCount(mystring));
}
return 0;
}