计算以' A'开头的单词数量。信件? - C

时间:2018-03-24 23:01:30

标签: c string algorithm count

我刚开始在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;
    }

3 个答案:

答案 0 :(得分:0)

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

你在这里。

#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;
}

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

答案 1 :(得分:-1)

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


int main() {

    char sentence[200];
    int i = 1;
    int counter = 0;
    printf("Enter sentence: ");

    fgets(sentence, 200, stdin);

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

    while( sentence[i]!='\n' )
    {
        if (sentence[i-1] == ' ' && sentence[i] == 'A')
            counter++;  // counter++ --- counter = counter +1;

        i++;
    }

    printf("No. of A in string is %d\n", counter);
    return 0;

}

答案 2 :(得分:-2)

在空格后用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++;

...