如何修改此程序以计算不是空格的字符数?

时间:2018-12-10 00:43:00

标签: c char

对于我的作业,我需要修改以下程序。我不能使用strings.h。

int main(void)
{
 int c, countSpaces = 0;

printf("Type sentence:\n");

do
{
  c = getchar();     
  if (c == ' ')
    countSpaces = countSpaces + 1;
}
while (c != '\n');     

printf("Sentence contains %d Spaces.\n", countSpaces);

return 0;
}

我尝试使用

if (c != EOF)
    countSpaces = countSpaces + 1;
}
while (c != '\n');     

printf("Sentence contains %d Spaces.\n", countSpaces - 1);

但是,这似乎是一种古怪而毫不含糊的方式。 谁能帮助我和/或向我解释如何做得更好?

预先感谢

4 个答案:

答案 0 :(得分:0)

  

我发布的代码对句子中的空格进行计数,我想对其进行修改以对输入句子中的所有字符进行计数。 – 21秒钟前fbN

if条件之外还有另一个计数器。

#include <stdio.h>

int main(void)
{
    int c;
    int countSpaces = 0;
    int countChars = 0;

    puts("Type sentence:");

    do {
        c = getchar();
        countChars += 1;
        if (c == ' ') {
            countSpaces += 1;
        }
    } while (c != '\n');     

    printf("Sentence contains %d spaces and %d characters.\n", countSpaces, countChars);

    return 0;
}

两个音符。 foo += 1foo = foo + 1的简写,没有foo++的复杂性。

无障碍ifwhile玩火。最终,您会不小心将其写出。

if( condition )
    do something
    whoops this is not in the condition but it sure looks like it is!

始终使用块形式。


$ ./test
Type sentence:
foo bar baz
Sentence contains 2 spaces and 12 characters.

请注意这是12,因为其中包括换行符。这是因为它正在检查{em>之后是什么c。您可以通过将c选中为已读来解决此问题。这是一个相当普通的“读取并检查” C循环习惯用法。

// Note, the parenthesis around `c = getchar()` are important.
while( (c = getchar()) != '\n' ) {
    countChars++;
    if (c == ' ') {
        countSpaces++;
    }
}
$ ./test
Type sentence:
foo bar baz
Sentence contains 2 spaces and 11 characters.

答案 1 :(得分:0)

我编写这段代码来计算给定字符串的长度,就像strlen函数一样。 而且我只用scanf,即使有空格,它也能很好地工作。

#include <stdio.h>

int main()
{
    char *str = calloc(sizeof(char),50);
    int i = 0, count = 0;

    printf("Type sentence:\n");
    scanf("%[^\n]",str);

    while (str[i++] != '\0')
        count++;                    //length of the string 

    printf("%d",count);
    return 0;
}

,如果您只想计算给定字符串中的字符,请使用以下代码:

#include <stdio.h>

int main()
{
    char *str = calloc(sizeof(char),50);
    int count = 0;

    printf("Type sentence:\n");
    scanf("%[^\n]",str);

    for (int i = 0; str[i] != '\0'; i++)
        if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
            count++;


    printf("Sentence contains %d characters.\n",count);
    return 0;
} 

输出:

Type sentence:                                                                                                             
hello world                                                                                                                
Sentence contains 10 characters.       

答案 2 :(得分:0)

从控制台(fgets())读取一行时,我总是喜欢使用stdin

#include <stdio.h>

int main(void)
{
    int i;
    int length = 0;
    char buffer[1024];

    printf( "Enter some text> " );
    fgets( buffer, sizeof(buffer), stdin );

    // If the user inputs > 1024 letters, buffer will not be \n terminated
    for ( i=0; buffer[i] != '\n' && buffer[i] != '\0'; i++ )
    {
        length += 1;
    }

    printf( "length: %d\n", length );

    return 0;
}

答案 3 :(得分:-1)

您可以像这样计算结果

int main(void)
{
    int c, countSpaces = 0;

    printf("Type sentence:\n");

    do
    {
        c = getchar();
        if (c == ' ')
        countSpaces++;
    }
    while (c != '\n');

    int countChar = c - countSpaces - 1 ; // -1 new line

    printf("Sentence contains %d Spaces.\n", countSpaces);
    printf("Sentence contains %d chars.\n", countChar);

    return 0;
}