Skip to main content

Sum of digits in a string

Here given code implementation process.

//C Program
//Sum of digits in a string 
#include <stdio.h>

//sum of string digits
void s_digit_sum(char str[],int size)
{
  int result = 0;

  for (int i = 0; i < size; ++i)
  {
    if(str[i]>='0' && str[i]<='9')
    {
      result+=str[i]-'0';
    }
  }

  printf("%d\n",result );
}


int main()
{
  char str1[]="a48cs1c8vb1";
  
  //Get the size
  int size=sizeof(str1)/sizeof(str1[0])-1;

  s_digit_sum(str1,size);

  //Each character as a single digits
  char str2[]="hns2-1-1001";
  
  //Get the size
  size=sizeof(str2)/sizeof(str2[0])-1;

  s_digit_sum(str2,size);
  return 0;
}

Output

22
5
// Java program
// Sum of digits in a string 

public class MyString {
  
  //sum of string digits
  public void s_digit_sum(String str)
  {
    int size = str.length();
    
    int result = 0;

    for (int i = 0; i < size; ++i)
    {
      if(str.charAt(i) >= '0' && str.charAt(i)<='9')
      {
        //Add digit sum
        result+=str.charAt(i)-'0';
      }
    }

    System.out.print(" "+result+"\n" );
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();

    obj.s_digit_sum("a48cs1c8vb1");

    obj.s_digit_sum("hns2-1-1001");
  }
}

Output

22
5
// C++ program
// Sum of digits in a string 
#include<iostream>

using namespace std;
class MyString {
	public:
    //sum of string digits
    void s_digit_sum(string str) {
      int size = str.size();
      int result = 0;
      for (int i = 0; i < size; ++i) {
        if (str[i] >= '0' &&
            str[i] <= '9') {
          //Add digit sum
          result += str[i] - '0';
        }
      }
      cout << " " << result << "\n";
    }
};
int main() {
	MyString obj = MyString();
	obj.s_digit_sum("a48cs1c8vb1");
	obj.s_digit_sum("hns2-1-1001");
	return 0;
}

Output

 22
 5
// C# program
// Sum of digits in a string 
using System;
public class MyString {
	//sum of string digits
	public void s_digit_sum(String str) {
		int size = str.Length;
		int result = 0;
		for (int i = 0; i < size; ++i) {
			if (str[i] >= '0' &&
				str[i] <= '9') {
				//Add digit sum
				result += str[i] - '0';
			}
		}
		Console.Write(" " + result + "\n");
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		obj.s_digit_sum("a48cs1c8vb1");
		obj.s_digit_sum("hns2-1-1001");
	}
}

Output

 22
 5
<?php
// Php program
// Sum of digits in a string 
class MyString {
	//sum of string digits

	public 	function s_digit_sum($str) {
		$size = strlen($str);
		$result = 0;
		for ($i = 0; $i < $size; ++$i) {
			if (ord($str[$i]) >= ord('0') &&
				ord($str[$i]) <= ord('9')) {
				//Add digit sum
				$result += ord($str[$i]) - ord('0');
			}
		}
		echo(" ". $result ."\n");
	}
}

function main() {
	$obj = new MyString();
	$obj->s_digit_sum("a48cs1c8vb1");
	$obj->s_digit_sum("hns2-1-1001");

}
main();

Output

 22
 5
// Node Js program
// Sum of digits in a string 
class MyString {
	//sum of string digits
	s_digit_sum(str) {
		var size = str.length;
		var result = 0;
		for (var i = 0; i < size; ++i) {
			if ((str[i]).charCodeAt(0) >= ('0').charCodeAt(0) &&
				(str[i]).charCodeAt(0) <= ('9').charCodeAt(0)) {
				//Add digit sum
				result += (str[i]).charCodeAt(0) - ('0').charCodeAt(0);
			}
		}

		process.stdout.write(" " + result + "\n");
	}
}

function main(args) {
	var obj = new MyString();
	obj.s_digit_sum("a48cs1c8vb1");
	obj.s_digit_sum("hns2-1-1001");
}

main();

Output

 22
 5
#  Python 3 program
#  Sum of digits in a string 
class MyString :
	# sum of string digits
	def s_digit_sum(self, str) :
		size = len(str)
		result = 0
		i = 0
		while (i < size) :
			if (ord(str[i]) >= ord('0') and ord(str[i]) <= ord('9')) :
				# Add digit sum
				result += ord(str[i]) - ord('0')
			
			i += 1
		
		print(" ", result )
	

def main() :
	obj = MyString()
	obj.s_digit_sum("a48cs1c8vb1")
	obj.s_digit_sum("hns2-1-1001")


if __name__ == "__main__":
	main()

Output

  22
  5
#  Ruby program
#  Sum of digits in a string 
class MyString 
	# sum of string digits
	def s_digit_sum(str) 
		size = str.length()
		result = 0
		i = 0
		while (i < size) 
			if ((str[i]).ord >= ('0').ord &&
				(str[i]).ord <= ('9').ord) 
				# Add digit sum
				result += (str[i]).ord - ('0').ord
			end
			i += 1
		end
		print(" ", result ,"\n")
	end
end
def main() 
	obj = MyString.new()
	obj.s_digit_sum("a48cs1c8vb1")
	obj.s_digit_sum("hns2-1-1001")
end
main()

Output

 22
 5
// Scala program
// Sum of digits in a string 
class MyString {
	//sum of string digits
	def s_digit_sum(str: String): Unit = {
		var size: Int = str.length();
		var result: Int = 0;
		var i: Int = 0;
		while (i < size) {
			if (str(i) >= '0' &&
				str(i) <= '9') {
				//Add digit sum
				result += str(i) - '0';
			}
			i += 1;
		}
		print(" " + result + "\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.s_digit_sum("a48cs1c8vb1");
		obj.s_digit_sum("hns2-1-1001");
	}
}

Output

 22
 5
// Swift program
// Sum of digits in a string 
class MyString {
	//sum of string digits
	func s_digit_sum(_ data: String) {
      	var str = Array(data);
		let size: Int = str.count;
		var result: Int = 0;
		var i: Int = 0;
		while (i < size) {
			if (str[i] >= "0" &&
				str[i] <= "9") {
				//Add digit sum
				result += Int(UnicodeScalar(String(str[i]))!.value  - UnicodeScalar("0")!.value);
			}
			i += 1;
		}
		print(" ", result );
	}
}
func main() {
	let obj: MyString = MyString();
	obj.s_digit_sum("a48cs1c8vb1");
	obj.s_digit_sum("hns2-1-1001");
}
main();

Output

  22
  5




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment