c - C字计数器和标点计数器
问题描述
我能够使用这种结构进行编程,并且承认我必须使用ispunct()
andisspace()
来计算标点符号和单词的数量。但我不确定如何使用这些功能进行编程。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int WordCount(char *input, int size)
{
//programming here//
}
int PunctuationCount(char *input, int size)
{
//programming here//
}
int main(void)
{
char test1[] = "Hello, World!";
char test2[] = "She sell sea shell on a sea shore. The shells she sells are sea-shells. I'm sure? For if she sells sea-shells on the sea-shore, then I'm sure she sells sea-shore shells!";
char test3[] = "VISION 2020 embodies every SKKU student's dream, will, and destiny to make SKKU a global leading university. To successfully establish VISION 2020, SKKU will pursue 'The 5 Core Strategies' and '5 Divisional Strategies'. The Strategic Tasks for 5 Major Areas are the general tasks that influence SKKU's competitiveness.";
printf("test1 has %d punctuations, %d words.\n", PunctuationCount(test1,sizeof(test1)/sizeof(char)), WordCount(test1,sizeof(test2)/sizeof(char)));
printf("test2 has %d punctuations, %d words.\n", PunctuationCount(test2,sizeof(test2)/sizeof(char)), WordCount(test2,sizeof(test2)/sizeof(char)));
printf("test3 has %d punctuations, %d words.\n", PunctuationCount(test3,sizeof(test3)/sizeof(char)), WordCount(test3,sizeof(test2)/sizeof(char)));
return 0;
}
解决方案
请注意,此算法不处理连字符或缩略词。(那是你的工作!)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int WordCount(char *input, int size)
{
int inword=0;
int count=0;
for (;*input; input++)
if (isalpha((int) *input))
{ // does not handle hyphenated words or contractions
inword=1;
}
else
{
if (inword)
{
inword=0;
count++;
}
}
return count;
}
int PunctuationCount(char *input, int size)
{
int count=0;
for (;*input; input++)
if (ispunct((int) *input))
{ // does not handle hyphenated words or contractions
count++;
}
return count;
}
int main(void)
{
char test1[] = "Hello, World!";
char test2[] = "She sell sea shell on a sea shore. The shells she sells are sea-shells. I'm sure? For if she sells sea-shells on the sea-shore, then I'm sure she sells sea-shore shells!";
char test3[] = "VISION 2020 embodies every SKKU student's dream, will, and destiny to make SKKU a global leading university. To successfully establish VISION 2020, SKKU will pursue 'The 5 Core Strategies' and '5 Divisional Strategies'. The Strategic Tasks for 5 Major Areas are the general tasks that influence SKKU's competitiveness.";
printf("test1 has %d punctuations, %d words.\n", PunctuationCount(test1,sizeof(test1)/sizeof(char)), WordCount(test1,sizeof(test2)/sizeof(char)));
printf("test2 has %d punctuations, %d words.\n", PunctuationCount(test2,sizeof(test2)/sizeof(char)), WordCount(test2,sizeof(test2)/sizeof(char)));
printf("test3 has %d punctuations, %d words.\n", PunctuationCount(test3,sizeof(test3)/sizeof(char)), WordCount(test3,sizeof(test2)/sizeof(char)));
return 0;
}
推荐阅读
- c# - 无法使用 NuGet FileHelpers.ExcelNPOIStorage 读取 Excel 文件
- css - CSS:100%响应矩形不滚动
- ruby - 万一我能回忆起“案例”吗?
- python - 如何将公式应用于 Dataframe 熊猫中的所有列
- swift - 在 UITableView 中传递和显示数据
- f# - 如何使用“onunload”事件向 SAFE Stack 中的服务器发送消息?
- flutter - 有没有办法自动弹出我的屏幕?
- javascript - index.js 中的 ReactDOM.render 不会呈现我的应用程序,而是会引发此错误:
- yaml - 空对象不会在合并中覆盖?
- angular - 调节拦截器