#include<stdio.h>#include<string.h>int get_word_nums(const char *str){ if(str==NULL) { puts("null pointer...\n"); return -1; } int count=0; char *p1=NULL; char *p2=(char*)str; while(*p2++!="\0") { if(p1==NULL&&*p2!=" ")//p1依次指向每個單詞的首字母 { p1=p2; } if(p1!=NULL&&*p2==" ")//一個單詞掃描結束 { count++; p1=NULL; } } return count;}int main(void){ puts("input your string:"); char str[1024]=""; gets(str); printf("nums:%d\n",get_word_nums(str)); return 0;}
#include<stdio.h>#include<string.h>int get_word_nums(const char *str){ if(str==NULL) { puts("null pointer...\n"); return -1; } int count=0; char *p1=NULL; char *p2=(char*)str; while(*p2++!="\0") { if(p1==NULL&&*p2!=" ")//p1依次指向每個單詞的首字母 { p1=p2; } if(p1!=NULL&&*p2==" ")//一個單詞掃描結束 { count++; p1=NULL; } } return count;}int main(void){ puts("input your string:"); char str[1024]=""; gets(str); printf("nums:%d\n",get_word_nums(str)); return 0;}