如何计算字符串C程序中的单词和标点符号?
我正在尝试用C语言编写一个程序,它可以计算字符串中的单词数和标点符号数,而不使用内置函数,如数组。是否可以在没有阵列的情况下执行此操作?另外,我的当前程序在下面,给了我一个初始化*word的错误,但我试图让它,用户输入一个字符串,程序计数,所以我不想初始化。非常感谢您的帮助如何计算字符串C程序中的单词和标点符号?,c,loops,for-loop,C,Loops,For Loop,我正在尝试用C语言编写一个程序,它可以计算字符串中的单词数和标点符号数,而不使用内置函数,如数组。是否可以在没有阵列的情况下执行此操作?另外,我的当前程序在下面,给了我一个初始化*word的错误,但我试图让它,用户输入一个字符串,程序计数,所以我不想初始化。非常感谢您的帮助 #include <stdio.h> #include<conio.h> int main(){ char *word; int countw
#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();
}
#包括
#包括
int main(){
字符*字;
int countword=0,i;
int countpunct=0,i;
printf(“\n输入字符串:”);
获取(单词);
对于(i=0;字[i]='';i++){
countword++;
}
对于(i=0;单词[i]='.| |'?'.| |'!'| |'('.| |')'.|'*'.| |'&')){
countpunct++;
}
printf(“\n字数为%d.”,countword);
printf(“\n标点符号marsks的数量为%d.”,countpunct);
getch();
}
一种方法是分别读取每个字符并进行处理
#include <stdio.h>
#if 0
#include<conio.h>
#endif
int main(){
int word;
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ') countword++;
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marsks is %d.", countpunct);
#if 0
getch();
#endif
}
#包括
#如果0
#包括
#恩迪夫
int main(){
int字;
int countword=0;
int countpunct=0;
printf(“\n输入字符串:”);
而((word=getchar())!=EOF&&word!='\n'){
如果(word='')countword++;
如果(单词=='。|单词=='?'|单词=='!'|单词=='('|单词==')'|单词=='*'|单词=='&'){
countpunct++;
}
}
printf(“\n字数为%d.”,countword);
printf(“\n标点符号marsks的数量为%d.”,countpunct);
#如果0
getch();
#恩迪夫
}
一种方法是分别读取每个字符并进行处理
#include <stdio.h>
#if 0
#include<conio.h>
#endif
int main(){
int word;
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ') countword++;
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marsks is %d.", countpunct);
#if 0
getch();
#endif
}
#包括
#如果0
#包括
#恩迪夫
int main(){
int字;
int countword=0;
int countpunct=0;
printf(“\n输入字符串:”);
而((word=getchar())!=EOF&&word!='\n'){
如果(word='')countword++;
如果(单词=='。|单词=='?'|单词=='!'|单词=='('|单词==')'|单词=='*'|单词=='&'){
countpunct++;
}
}
printf(“\n字数为%d.”,countword);
printf(“\n标点符号marsks的数量为%d.”,countpunct);
#如果0
getch();
#恩迪夫
}
一种方法是分别读取每个字符并进行处理
#include <stdio.h>
#if 0
#include<conio.h>
#endif
int main(){
int word;
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ') countword++;
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marsks is %d.", countpunct);
#if 0
getch();
#endif
}
#包括
#如果0
#包括
#恩迪夫
int main(){
int字;
int countword=0;
int countpunct=0;
printf(“\n输入字符串:”);
而((word=getchar())!=EOF&&word!='\n'){
如果(word='')countword++;
如果(单词=='。|单词=='?'|单词=='!'|单词=='('|单词==')'|单词=='*'|单词=='&'){
countpunct++;
}
}
printf(“\n字数为%d.”,countword);
printf(“\n标点符号marsks的数量为%d.”,countpunct);
#如果0
getch();
#恩迪夫
}
一种方法是分别读取每个字符并进行处理
#include <stdio.h>
#if 0
#include<conio.h>
#endif
int main(){
int word;
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ') countword++;
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marsks is %d.", countpunct);
#if 0
getch();
#endif
}
#包括
#如果0
#包括
#恩迪夫
int main(){
int字;
int countword=0;
int countpunct=0;
printf(“\n输入字符串:”);
而((word=getchar())!=EOF&&word!='\n'){
如果(word='')countword++;
如果(单词=='。|单词=='?'|单词=='!'|单词=='('|单词==')'|单词=='*'|单词=='&'){
countpunct++;
}
}
printf(“\n字数为%d.”,countword);
printf(“\n标点符号marsks的数量为%d.”,countpunct);
#如果0
getch();
#恩迪夫
}
有更多的代码行,但是switch
语句不是一个坏方法。下面代码的总体思路应该是可行的
#include <stdio.h>
#include <string.h> //for strlen()
int main(){
char input[255];
int wcount, pcount, i;
wcount = pcount = 0;
printf("\nEnter the String: ");
fgets(input, 255, stdin); //use this instead
for (i=0; i < strlen(input); i++){
switch (input[i]){
case ' ':
if (i > 0) wcount++;
break;
case '.':
case '?':
case '!':
case '(':
case ')':
case '*':
case '&':
pcount++;
break;
}
}
return 0;
}
#包括
#包括//用于strlen()
int main(){
字符输入[255];
int wcount、pcount、i;
wcount=pcount=0;
printf(“\n输入字符串:”);
fgets(input,255,stdin);//改用这个
对于(i=0;i0)wcount++;
打破
案例“”:
案例“?”:
案例“!”:
格“(”:
案例“)”:
案例“*”:
案例“&”:
pcount++;
打破
}
}
返回0;
}
有更多的代码行,但是switch
语句不是一个坏方法。下面代码的总体思路应该是可行的
#include <stdio.h>
#include <string.h> //for strlen()
int main(){
char input[255];
int wcount, pcount, i;
wcount = pcount = 0;
printf("\nEnter the String: ");
fgets(input, 255, stdin); //use this instead
for (i=0; i < strlen(input); i++){
switch (input[i]){
case ' ':
if (i > 0) wcount++;
break;
case '.':
case '?':
case '!':
case '(':
case ')':
case '*':
case '&':
pcount++;
break;
}
}
return 0;
}
#包括
#包括//用于strlen()
int main(){
字符输入[255];
int wcount、pcount、i;
wcount=pcount=0;
printf(“\n输入字符串:”);
fgets(input,255,stdin);//改用这个
对于(i=0;i0)wcount++;
打破
案例“”:
案例“?”:
案例“!”:
格“(”:
案例“)”:
案例“*”:
案例“&”:
pcount++;
打破
}
}
返回0;
}
有更多的代码行,但是switch
语句不是一个坏方法。下面代码的总体思路应该是可行的
#include <stdio.h>
#include <string.h> //for strlen()
int main(){
char input[255];
int wcount, pcount, i;
wcount = pcount = 0;
printf("\nEnter the String: ");
fgets(input, 255, stdin); //use this instead
for (i=0; i < strlen(input); i++){
switch (input[i]){
case ' ':
if (i > 0) wcount++;
break;
case '.':
case '?':
case '!':
case '(':
case ')':
case '*':
case '&':
pcount++;
break;
}
}
return 0;
}
#包括
#包括//用于strlen()
int main(){
字符输入[255];
int wcount、pcount、i;
wcount=pcount=0;
printf(“\n输入字符串:”);
fgets(input,255,stdin);//改用这个
对于(i=0;i0)wcount++;
打破
案例“”:
案例“?”:
案例“!”:
格“(”:
案例“)”:
案例“*”:
案例“&”:
pcount++;
打破
}
}
返回0;
}
有更多的代码行,但是switch
语句不是一个坏方法。下面代码的总体思路应该是可行的
#include <stdio.h>
#include <string.h> //for strlen()
int main(){
char input[255];
int wcount, pcount, i;
wcount = pcount = 0;
printf("\nEnter the String: ");
fgets(input, 255, stdin); //use this instead
for (i=0; i < strlen(input); i++){
switch (input[i]){
case ' ':
if (i > 0) wcount++;
break;
case '.':
case '?':
case '!':
case '(':
case ')':
case '*':
case '&':
pcount++;
break;
}
}
return 0;
}
#包括
#包括//用于strlen()
int main(){
字符输入[255];