[英]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.