【问题标题】:Counting number of words which starts with 'A' letter? - C计算以“A”字母开头的单词数? - C
【发布时间】:2018-09-03 09:08:58
【问题描述】:

我是在 Java 之后才开始学习 C 的,所以对我来说有点困惑。我试图编写一个程序来计算以“A”字母开头的单词的数量。问题是它只读取我输入的第一个单词而忽略了句子的其余部分。有人可以帮我解决这个问题吗?我会很感激的。

#include <stdio.h>
#include <string.h>

void main() {

    char sentence[200];
    int i;
    int counter = 0;
    printf("Enter sentence: ");
    scanf("%s", &sentence);

    for (i = 0; sentence[i] != 0, sentence[i] != ' '; i++){
            if (sentence[i] == 'A') {
                counter = counter +1;
            }
        }
        printf("No. of A in string %s > %d\n", sentence, counter);
        return 0;
    }

【问题讨论】:

  • 我投票结束这个问题,因为它没有显示出最少的研究工作。
  • 我是初学者,不知道问是犯罪行为。人们有时很粗鲁。至少我愿意向比我更了解的人和我的错误学习。
  • @CruelWorld 问这个问题不是犯罪,但 C 语言中的字符串拆分已在本网站(以及 数十个 其他网站! )。强烈建议不要提出重复的问题,并且这里基本上需要进行最少的研究。
  • @AndreFigueiredo 公平点,必须回到审核队列最佳实践。

标签: c string algorithm count


【解决方案1】:

strstr 扫描字符串以查找空格后以“A”开头的单词,然后查找字符串中的第一个单词:

...
fgets(sentence, sizeof(sentence), stdin);

int count = 0;
const char *tmp = sentence;

while(tmp = strstr(tmp, " A")) {
   count++;
   tmp++;
}

if (sentence[0] == 'A') count++;

...

【讨论】:

  • 你忘了输入。
  • 因为很明显
  • 我明白了。谢谢你们的帮助,谢谢你们不像上面的人那样刻薄。
  • 接受输入对 OP 来说并不明显。他将scanf("%s",...) 误用于带有空格的字符串。
  • 不错,我已经添加了
【解决方案2】:

我们初学者应该互相帮助。:)

你来了。

#include <stdio.h>
#include <string.h>

int main(void) 
{
    enum { N = 200 };
    char sentence[N];

    printf( "Enter sentence: " );
    fgets( sentence, N, stdin );

    size_t n = 0;

    for ( const char *p = sentence; *p; p += strcspn( p,  " \t" ) )
    {
        p += strspn( p, " \t" );
        if ( *p == 'A' ) ++n;
    }

    printf("No. of A in string \"%s\" is %zu\n", sentence, n );

    return 0;
}

程序输出可能看起来像

Enter sentence: Any word that starts with A
No. of A in string "Any word that starts with A" is 2

另外,最好将字符串文字 " \t" 替换为命名变量。

例如

#include <stdio.h>
#include <string.h>

int main(void) 
{
    enum { N = 200 };
    char sentence[N];
    char c = 'A';

    const char *blank = " \t";

    printf( "Enter sentence: " );
    fgets( sentence, N, stdin );

    size_t n = 0;

    for ( const char *p = sentence; *p; p += strcspn( p,  blank ) )
    {
        p += strspn( p, blank );
        if ( *p == c ) ++n;
    }

    printf("No. of %c in string \"%s\" is %zu\n", c, sentence, n );

    return 0;
}

您可以编写一个单独的函数来完成该任务。函数声明如下所示

size_t count_words_start_with( const char *s, char c );

这是一个演示程序。

#include <stdio.h>
#include <string.h>

 size_t count_words_start_with( const char *s, char c )
 {
    const char *blank = " \t";
    size_t n = 0;

    for ( const char *p = s; *p; p += strcspn( p,  blank ) )
    {
        p += strspn( p, blank );
        if ( *p == c ) ++n;
    }

    return n;
 }

int main(void) 
{
    enum { N = 200 };
    char sentence[N];
    char c = 'A';

    printf( "Enter sentence: " );
    fgets( sentence, N, stdin );


    printf("No. of %c in string \"%s\" is %zu\n", 
        c, sentence, count_words_start_with( sentence, c ) );

    return 0;
}

考虑到我的答案几乎总是最好的答案。:)

【讨论】:

    猜你喜欢
    • 2023-02-21
    • 2022-01-08
    • 2014-01-02
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多