Topic: Sum of digits

This is a very easy problem to solve. Just for practice!

Find the sum of the digits in the following string -

dfjksdj32423skfjsdfj78shdfjsh0hf3bsdjf3jfdsfj134343fbjdsfkjsdbfsjd017834sdfsdjfdsjfk972347jkhsfdjhsd34jhgha766afsdkfjdjfdfbsdj

Feel free to use any language...

Last edited by nsmgr8 (March 17, 2009 12:28 am)

Re: Sum of digits

<?php
 
$string = 'dfjksdj32423skfjsdfj78shdfjsh0hf3bsdjf3jfdsfj134343fbjdsfkjsdbfsjd017834sdfsdjfdsjfk972347jkhsfdjhsd34jhgha766afsd';
$size = str_split($string);
foreach($size as $key)
{
    if(is_numeric($key))
        $total = $total + $key;
}
echo $total;
?>

i think it's done smile

http://tareq.wedevs.com/test/sig/blog_update.php
http://tareq.wedevs.com/test/sig/twitt_update.php

Re: Sum of digits

Yes, that's well done. Now let's see how do you do it in Python.

s = 'dfjksdj32423skfjsdfj78shdfjsh0hf3bsdjf3jfdsfj134343fbjdsfkjsdbfsjd017834sdfsdjfdsjfk972347jkhsfdjhsd34jhgha766afsd'
print sum([int(i) for i in s if i in '1234567890'])

Cool, no?

It's just plain English! If you read it loud, you'll read -

Print sum of int of i for i in s if i in one, two, three, four, five, six, seven, eight, nine, zero.

Last edited by nsmgr8 (March 18, 2009 9:23 pm)

Re: Sum of digits

wow surprised , it's really great like English language. waiting

http://tareq.wedevs.com/test/sig/blog_update.php
http://tareq.wedevs.com/test/sig/twitt_update.php

Re: Sum of digits

in c

#include<stdio.h>
#include<string.h>
 
int main()
{
    char a[100];
    int i,sum=0,L;
 
    gets(a);
    L=strlen(a);
 
    for (i=0;i<L;i++)
    {
        if(a[i]>='0' && a[i]<='9')       
        sum=sum+a[i]-48;
    }
 
    printf("%d",sum);
    return 0;
}

Once i wrote the code for detecting numbers between string and add....like..12sd1er7..sum should be 20. But, i think, i lost it. cry

Re: Sum of digits

wrote that again smile

#include<stdio.h>
#include<string.h>
 
int main()
{
    char a[100];
    int i,sum=0,L,digit=0;
 
    gets(a);
    L=strlen(a);
 
    for (i=0;i<L;i++)
    {
        if(a[i]>='0' && a[i]<='9')       
        {
          digit=digit+a[i]-48;
          if(a[i+1]>='0' && a[i+1]<='9')
          digit=digit*10;
          else
          {
              sum=sum+digit;
              digit=0;
          }
 
    }   
 
 
    }
 
    printf("%d",sum);
    return 0;
}

it will find all "int" from a string and will add them.

is it ok sopnochari vaia?

Re: Sum of digits

@mahdee vai, sum is 134 big_smile

http://tareq.wedevs.com/test/sig/blog_update.php
http://tareq.wedevs.com/test/sig/twitt_update.php

Re: Sum of digits

mahdee, sum of integers is nice. Can you do it with float number now? I mean if you have "asd34.5hf23", then the result is 57.5.

Never use gets() for input. This is very dangerous. There are numerous thing can happen with it. And you cannot find the bug in your software if you use it.

Re: Sum of digits

i know the denger with that gets(). It holds the enter as an input and if i take input with scanf or somthing, then the first input i get, should be blank. I faced this problem and that took a lot time to find , what the bug was.

I ll try that float problem tonight smile

Re: Sum of digits

Here is a simpler solution to find the sum of integers in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
    int l, loc = 0, sum = 0;
    char s[] = "dfjksdj32423skfjsdfj78shdfjsh0hf3bsdjf3jfdsfj134343fbjdsfkjsdbfsjd017834sdfsdjfdsjfk972347jkhsfdjhsd34jhgha766afsdkfjdjfdfbsdj";
    char d[] = "1234567890";
 
    l = strlen(s);
 
    while(loc < l) {
        loc += strcspn(s+loc, d);
        sum += atoi(s+loc);
        loc += strspn(s+loc, d);
    }
 
    printf("%d\n", sum);
 
    return 0;
}

Re: Sum of digits

#include<stdio.h>
 
int main()
{
    char a[100];
    int i,L,count=1,j;
    float sum=0,digit=0,point=0,sump=0;
 
    for(i=0;i<100;i++)
    {
    scanf("%c",&a[i]);
    if(a[i]=='\n')
    break;
    }   
 
    L=i; //strlen
 
    for (i=0;i<L;i++)
    {
        if(a[i]>='0' && a[i]<='9')       
        {
          digit=digit+a[i]-48;
          if(a[i+1]>='0' && a[i+1]<='9')
          digit=digit*10;
          else
          {
              sum=sum+digit;
              digit=0;
          }}
 
 
 
 
 
 
          if(a[i]=='.')
          {
          for(i=i+1;a[i]>='0' && a[i]<='9';i++)
          {
          point=point+a[i]-48;
 
          if(a[i+1]>='0' && a[i+1]<='9')
          {
             point=point*10;
             count++;
          }
          else
          {   for(j=0;j<count;j++)
              point=point/10;
 
              sump=sump+point;
              point=0;
              count=1;
          }
          }     
          }
  }
 
    printf("%f",sum+sump);
    return 0;
}

finally done hairpull love

Re: Sum of digits

he he he, Nasim vai's solution is so small. I did not tried in C thinking

http://tareq.wedevs.com/test/sig/blog_update.php
http://tareq.wedevs.com/test/sig/twitt_update.php

Re: Sum of digits

i haven't used "stdlib.h" till brokenheart. i think, i should start using it . neutral

Re: Sum of digits

good works:)

Azad

Re: Sum of digits

Dont know How it will look like.

int main(){
char c;
int sum=0;
while(scanf("%c",&c), c!='\n') sum += (c-'0')*(c>'0' && c<='9');
printf("Sum: %d\n",sum);
}

Last edited by shiplu (March 19, 2009 9:33 am)

http://url.ie/zybhttp://url.ie/zydhttp://url.ie/zyc
মুখে তুলে কেউ খাইয়ে দেবে না। নিজের হাতেই সেটা করতে হবে।

Re: Sum of digits

wow.

Do (c>'0' && c<='9') (or such other  conditions ) always return 1 or 0?

Re: Sum of digits

Yes, boolean operations always return 0 or 1.

Your float sum is also working. Nice!