Add the three digit numbers in a given text of string using c

899 views Asked by At
#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

There are 4 answers

0
Gajendra Bagali On

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:

`memset(p,'\0',sizeof(p));`

Let me know, if you have any concerns.

0
David C. Rankin On

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).

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 )

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.

#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
0
Mikkel Christiansen On
#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.

0
Shreevardhan On

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