#include <stdio.h>
#include <string.h>
int add(char s[])
{
char p[3];
int i=0, j=0, sum=0;
for(i=0;s[i]!='
#include <stdio.h>
#include <string.h>
int add(char s[])
{
char p[3];
int i=0, j=0, sum=0;
for(i=0;s[i]!='\0';i++)
{
if(isdigit(s[i])&&isdigit(s[i+1])&&isdigit(s[i+2])&&!isdigit(s[i+3])&&!isdigit(s[i-1]))
{
p[0]=s[i];
p[1]=s[i+1];
p[2]=s[i+2];
sum+=atoi(p);
}
}
return sum;
}
Above I tried writing the code to add only three digit numbers within the string text but it is not working. Can't figure out what the problem is.
上面我尝试编写代码,在字符串文本中只添加三位数字,但它不起作用。无法弄清问题是什么。
4 个解决方案
#1
1
If I understand you want to add the sum of the first 3 digits in the string, then you are definitely going about it the hard way. After passing the string to your function, simply assign a pointer to the string and check each char in the string. If the char is a digit, then add the digit to sum. After you have found your 3 digits, simply return the sum. (you may as well make your function general to return the sum of any number of digits you choose).
如果我理解你想要在字符串中添加前三位数的总和,那么你肯定是在艰难的方式。将字符串传递给函数后,只需指定一个指向字符串的指针并检查字符串中的每个字符。如果char是数字,则将数字添加到sum。找到3位数字后,只需返回总和即可。 (您也可以使您的函数一般返回您选择的任意位数的总和)。
Note: you must convert the ascii value of the digit to it numeric value before adding it to sum. (i.e. ascii char 9 - '0' is numeric 9, etc..) (See the ascii character values )
注意:在将数字的ascii值添加到sum之前,必须将其转换为数字值。 (即ascii char 9 - '0'是数字9等。)(参见ascii字符值)
Here is a short example that adds the first 3 digits found in the string using the method above. If you have questions or different needs, just let me know.
这是一个简短的例子,它使用上面的方法添加字符串中的前3位数字。如果您有任何疑问或需求,请告诉我。
#include <stdio.h>
#include <string.h>
int add_ndigits (const char *s, size_t ndigits)
{
const char *p = s; /* pointer to string */
int sum = 0;
size_t count = 0;
while (*p) { /* for each char in string */
if (*p >= '0' && *p <= '9') { /* check if it is a digit */
sum += *p - '0'; /* if so add value to sum */
count++; /* increment digit count */
if (count == ndigits) /* if count = ndigits break */
break;
}
p++;
}
return sum; /* return the sum of the first ndigits in string */
}
int main (void) {
char string[] = "this is 1 string with 2 or 3 more digits like 1, 2, 7, etc.";
int sum3 = add_ndigits (string, 3);
printf ("\n The sum of the first 3 digits in 'string' is: %d\n\n", sum3);
return 0;
}
Output
产量
$ ./bin/add3string
The sum of the first 3 digits in 'string' is: 6
#2
1
Simple alternative
简单的选择
int add(char s[]) {
int c = 0, ans = 0;
for (const char * d = s; *d; ++d)
if (isdigit(*d) && c < 3) {
ans += (*d - '0');
++c;
}
return ans;
}
#3
0
char p[3] can not hold 3 characters. You need to have extra byte for the null terminator. You can declare it as char char p[4] and do memset to avoid confusion with null termination as following:
char p [3]不能容纳3个字符。您需要为null终止符添加额外的字节。您可以将其声明为char char p [4]并执行memset以避免与null终止混淆,如下所示:
`memset(p,'\0',sizeof(p));`
Let me know, if you have any concerns.
如果您有任何疑虑,请告诉我。
#4
0
#include <stdio.h>
#include <string.h>
int add(char s[])
{
char *p;
unsigned sum=0;
do {
while(!isdigit(*s) && *s) /* Skip nondigits except \0 */
++s;
p=s;
if(isdigit(*s) && isdigit(*++s) && isdigit(*++s))
if(isdigit(*++s)) /* more than 3 digits? */
while(isdigit(*++s)) /* then skip rest of digits */
{}
else {
if(*p == '0') { /* prevent atoi from reading octal */
if(*++p == '0')
++p;
}
sum += atoi(p);
}
} while(*s);
return sum;
}
EDIT: I'm soo stupid. I never liked the
编辑:我太傻了。我从来都不喜欢
if(isdigit(p[0]=s[i]) && isdigit(p[1]=s[++i]) && isdigit(p[2]=s[++i]))
to begin with.
首先。
';i++)
{
if(isdigit(s[i])&&isdigit(s[i+1])&&isdigit(s[i+2])&&!isdigit(s[i+3])&&!isdigit(s[i-1]))
{
p[0]=s[i];
p[1]=s[i+1];
p[2]=s[i+2];
sum+=atoi(p);
}
}
return sum;
}
#include <stdio.h>
#include <string.h>
int add