简体   繁体   English

我正在尝试计算 C 中字符串中的数字

[英]I'm trying to count the numbers in a string in C

What's wrong with my code?我的代码有什么问题? I'm trying to count the numbers in a string after skipping all alphabets.跳过所有字母后,我试图计算字符串中的数字。

How to skip the alphabets and only count the numbers?如何跳过字母表只计算数字?

#include <stdio.h>
#include <stdlib.h>

void function(char a[])
{
    int i=0,count=0;
    while(a[i]!='\0')
    {
        if(a[i]>='a'&&a[i]<='z')
        {
            continue;
        }
        else
        {
            count++;
        }
        i++;
    }
    printf("%d",count);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[100001];
        scanf("%s",a);
        function(a);
        printf("\n");
    }
}

I switched your usage of scanf to use fgets .我将您对scanf的使用切换为使用fgets This works:这有效:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

void function(char a[])
{
    int i=0,count=0;
    while(a[i]!='\0')
    {
        if (isdigit(a[i]))
        {
            count++;
        }
        i++;
    }
    printf("%d",count);
}

int main()
{
    int t = 0;

    scanf("%d", &t);

    while(t--)
    {
        char a[100001];
        fgets(a, sizeof(a), stdin);
        function(a);
        puts("\n");
    }
}

If the OP's intent was in fact to count up the groups of digits (in which the definition of 'number' is each contiguous run of digits), this will accomplish the goal:如果 OP 的意图实际上是计算数字组(其中“数字”的定义是每个连续的数字),这将实现目标:

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

int function(char a[])
{
    size_t i = 0;
    int cnt = 0;
    //  convert all non-digits to whitespace
    printf("Converting\n");
    for (char *p = a; *p != '\0'; ++p)
    {
        *p = isdigit(*p) ? *p : ' ';
    }

    printf("Found Groups: %s\n",a);
    // note: strtok mutates the string it is given, it will not be usable after this:
    for (char *gr = strtok(a," "); gr != NULL; gr = strtok(NULL, " "))
    {
        cnt++;
    }
    printf("Number of Groups: %d\n", cnt);

}
int main()
{
    printf("Enter strings (Ctrl-C to end)\n");
    while(1)
    {
        int result = 0;
        char a[100001];
        scanf("%s",a);
        result = function(a);
        printf("\n");
    }
}

(I was not able to get the version using fgets to work, but how the string is gathered is not really in the scope of the question, the OP's original code in main is functional, I just made it an open-ended loop for my tests) (我无法让使用fgets的版本工作,但是字符串是如何收集的并不是问题的 scope,OP 在main中的原始代码是功能性的,我只是让它成为我的开放式循环测试)

In your code continue statement is responsible for wrong answer.在您的代码中,continue 语句负责错误的答案。 Because of it your code is not encountering the statement a[i].='\0'.因此,您的代码不会遇到语句 a[i].='\0'。 so suggest you to just avoid it and close your if statement without any code as like me.所以建议你避免它并像我一样关闭你的 if 语句而没有任何代码。 One more suggestion use also uppercase letters as user can also enter uppercase letters and using fgets() function you can also read white spaces.另一个建议也使用大写字母,因为用户也可以输入大写字母,使用 fgets() function 也可以读取空格。 Try out this试试这个

#include <stdio.h>
#include <stdlib.h>

void function(char a[])
{
   int i=0,count=0;
  while(a[i]!='\0')
 {
        if((a[i]>='a'&&a[i]<='z') || (a[i]>='A'&&a[i]<='Z'));
       else
      {
         count++;
        }
        i++;
    }
    printf("%d",count);
}

int main()
{
   int t;
  scanf("%d",&t);
 while(t--)
    {
       char a[100001];
      scanf("%s",a);
     function(a);
        printf("\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使字符串仅接受字母和空格,但不带数字 - I'm trying to make a string accept only letters and space BUT NO NUMBERS 我正在尝试使用strcmp()在C中对字符串数组进行排序 - I'm trying to use strcmp() to sort a string array in C 我正在尝试用 c 编写格式字符串(命令行工具) - I'm trying to write a format string in c (command line tool) 为什么在 C 中修改了字符串,即使我没有尝试修改它? - Why is string modified in C even when I'm not trying to modify it? 试图从 C 中的字符串中删除所有数字 - Trying to remove all numbers from a string in C 试图计算C字符串中有多少个字符 - Trying to count how many characters there are in a string in C 我正在尝试将数字添加到每个新行 - I'm trying to add numbers to each new line 所以,我试图用 C 语言在文本文件上写一些浮点数,但它只写了一些 0.000 0.000 - So, I'm trying to write some float numbers on a text file in C language , but it is only writing some 0.000 0.000 我正在尝试在 C 中设计一个词法分析器 - I'm trying to design a lexical Analyzer in C 我正在尝试编写C代码对String进行排序,但始终在第13行显示错误消息 - I'm trying to write a C code to sort String, but there always shows an error message in line 13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM