【发布时间】:2015-09-13 05:29:33
【问题描述】:
我正在尝试用 C 语言编写一个程序,该程序计算字符串中的单词和标点符号的数量,而不使用数组等内置函数。没有数组可以这样做吗?另外,我当前的程序在下面,给了我一个初始化 *word 的错误,但我试图让用户输入一个字符串并且程序对其进行计数,所以我不想初始化。非常感谢您的帮助!
#include <stdio.h>
#include<conio.h>
int main(){
char *word;
int countword = 0, i;
int countpunct = 0, i;
printf("\nEnter the String: ");
gets(word);
for (i = 0; word[i] == ' '; i++){
countword++;
}
for (i = 0; word[i] == '.' || '?' || '!' || '(' || ')' || '*' || '&'){
countpunct++;
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marsks is %d.", countpunct);
getch();
}
【问题讨论】:
-
那么长的 OR
||序列在 C 中不是正确的语法。