[英]Program to count length of each word in string in C
I'm writting a program to count the length of each word in array of characters. 我正在编写一个程序来计算字符数组中每个单词的长度。 I was wondering if You guys could help me, because I'm struggling with it for at least two hours for now and i don't know how to do it properly.
我想知道你们是否可以帮助我,因为我现在至少要努力两个小时,而且我不知道该怎么做。 It should go like that:
它应该像这样:
(number of letters) - (number of words with this many letters) (字母数)-(具有这么多个字母的字数)
2 - 1 2-1
3 - 4 3-4
5 - 1 5-1
etc. 等等
char tab[1000];
int k = 0, x = 0;
printf("Enter text: ");
fgets(tab, 1000, stdin);
for (int i = 2; i < (int)strlen(tab); i++)
{
for (int j = 0; j < (int)strlen(tab); j++)
{
if (tab[j] == '\0' || tab[j]=='\n')
break;
if (tab[j] == ' ')
k = 0;
else k++;
if (k == i)
{
x++;
k = 0;
}
}
if (x != 0)
{
printf("%d - %d\n", i, x);
x = 0;
k = 0;
}
}
return 0;
By using two for
loops, you're doing len**2
character scans. 通过使用两个
for
循环,您可以进行len**2
字符的扫描。 (eg) For a buffer of length 1000, instead of 1000 character comparisons, you're doing 1,000,000 comparisons. (例如)对于长度为1000的缓冲区,而不是1000个字符的比较,您要进行1,000,000个比较。
This can be done in a single for
loop if we use a word length histogram array. 如果我们使用字长直方图数组,则可以在单个
for
循环中完成。
The basic algorithm is the same as your inner loop. 基本算法与您的内部循环相同。
When we have a non-space character, we increment a current length value. 当我们有一个非空格字符时,我们增加当前长度值。 When we see a space, we increment the histogram cell (indexed by the length value) by 1. We then set the length value to 0.
当我们看到一个空间时,我们将直方图像元(由长度值索引)增加1。然后将长度值设置为0。
Here's some code that works: 这是一些有效的代码:
#include <stdio.h>
int
main(void)
{
int hist[100] = { 0 };
char buf[1000];
char *bp;
int chr;
int curlen = 0;
printf("Enter text: ");
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
bp = buf;
for (chr = *bp++; chr != 0; chr = *bp++) {
if (chr == '\n')
break;
// end of word -- increment the histogram cell
if (chr == ' ') {
hist[curlen] += 1;
curlen = 0;
}
// got an alpha char -- increment the length of the word
else
curlen += 1;
}
// catch the final word on the line
hist[curlen] += 1;
for (curlen = 1; curlen < sizeof(hist) / sizeof(hist[0]); ++curlen) {
int count = hist[curlen];
if (count > 0)
printf("%d - %d\n",curlen,count);
}
return 0;
}
UPDATE: 更新:
and i don't really understand pointers.
而且我不太了解指针。 Is there any simpler method to do this?
有没有更简单的方法可以做到这一点?
Pointers are a very important [essential] tool in the C arsenal, so I hope you get to them soon. 指针是军械库中非常重要的[必要]工具,所以希望您能尽快找到它们。
However, it is easy enough to convert the for
loop (Removing the char *bp;
and bp = buf;
): 但是,转换
for
循环很容易(除去char *bp;
和bp = buf;
):
Change: 更改:
for (chr = *bp++; chr != 0; chr = *bp++) {
Into: 进入:
for (int bufidx = 0; ; ++bufidx) {
chr = buf[bufidx];
if (chr == 0)
break;
The rest of the for
loop remains the same. for
循环的其余部分保持不变。
Here's another loop [but, without optimization by the compiler] double fetches the char: 这是另一个循环[但是,没有经过编译器的优化]双重获取char:
for (int bufidx = 0; buf[bufidx] != 0; ++bufidx) {
chr = buf[bufidx];
Here is a single line version. 这是单行版本。 Note this is not recommended practice because of the embedded assignment of
chr
inside the loop condition clause, but is for illustration purposes: 请注意,这不建议,因为嵌入式分配的做法
chr
循环条件子句中 ,但为了说明:
for (int bufidx = 0; (chr = buf[bufidx]) != 0; ++bufidx) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.