Calculate sum of all numbers present in a string

Here given code implementation process.

/*
    C program for
    Calculate sum of all numbers present in a string
*/
#include <stdio.h>
#include <string.h>

// Check that if value is number digit or not
int isNumber(char value)
{
	if (value >= '0' && value <= '9')
	{
		return 1;
	}
	return 0;
}
void sumOfNumbers(char *text)
{
	int n = strlen(text);
	// Declare some auxiliary variables
	int sum = 0;
	int num = 0;
	int i = 0;
	// Execute this loop thought by size of text
	while (i < n)
	{
		// Collect number
		while (i < n && isNumber(text[i]))
		{
			num = (num *10) + (text[i] - '0');
			// Increase index value
			i++;
		}
		// Add number
		sum += num;
		// Reset the value of num
		num = 0;
		// Increase index value
		i++;
	}
	printf(" Given Text : %s", text);
	printf("\n Sum : %d \n", sum);
}
int main(int argc, char
	const *argv[])
{
	sumOfNumbers("u42p2dsf12");
	sumOfNumbers("1gsar2cs");
	return 0;
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3

/*
    Java program for
    Calculate sum of all numbers present in a string
*/
public class NumberSum
{
	// Check that if value is number digit or not
	public boolean isNumber(char value)
	{
		if (value >= '0' && value <= '9')
		{
			return true;
		}
		return false;
	}
	public void sumOfNumbers(String text)
	{
		int n = text.length();
		// Declare some auxiliary variables
		int sum = 0;
		int num = 0;
		int i = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && isNumber(text.charAt(i)))
			{
				num = (num * 10) + (text.charAt(i) - '0');
				// Increase index value
				i++;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i++;
		}
		System.out.print(" Given Text : " + text);
		System.out.println("\n Sum : " + sum);
	}
	public static void main(String[] args)
	{
		NumberSum task = new NumberSum();
		// Test
		task.sumOfNumbers("u42p2dsf12");
		task.sumOfNumbers("1gsar2cs");
	}
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Calculate sum of all numbers present in a string
*/
class NumberSum
{
	public:
		// Check that if value is number digit or not
		bool isNumber(char value)
		{
			if (value >= '0' && value <= '9')
			{
				return true;
			}
			return false;
		}
	void sumOfNumbers(string text)
	{
		int n = text.length();
		// Declare some auxiliary variables
		int sum = 0;
		int num = 0;
		int i = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && this->isNumber(text[i]))
			{
				num = (num *10) + (text[i] - '0');
				// Increase index value
				i++;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i++;
		}
		cout << " Given Text : " << text;
		cout << "\n Sum : " << sum << endl;
	}
};
int main()
{
	NumberSum *task = new NumberSum();
	// Test
	task->sumOfNumbers("u42p2dsf12");
	task->sumOfNumbers("1gsar2cs");
	return 0;
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
// Include namespace system
using System;
/*
    Csharp program for
    Calculate sum of all numbers present in a string
*/
public class NumberSum
{
	// Check that if value is number digit or not
	public Boolean isNumber(char value)
	{
		if (value >= '0' && value <= '9')
		{
			return true;
		}
		return false;
	}
	public void sumOfNumbers(String text)
	{
		int n = text.Length;
		// Declare some auxiliary variables
		int sum = 0;
		int num = 0;
		int i = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && this.isNumber(text[i]))
			{
				num = (num * 10) + (text[i] - '0');
				// Increase index value
				i++;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i++;
		}
		Console.Write(" Given Text : " + text);
		Console.WriteLine("\n Sum : " + sum);
	}
	public static void Main(String[] args)
	{
		NumberSum task = new NumberSum();
		// Test
		task.sumOfNumbers("u42p2dsf12");
		task.sumOfNumbers("1gsar2cs");
	}
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
package main
import "fmt"
/*
    Go program for
    Calculate sum of all numbers present in a string
*/

// Check that if value is number digit or not
func  isNumber(value byte) bool {
	if value >= '0' && value <= '9' {
		return true
	}
	return false
}
func  sumOfNumbers(text string) {
	var n int = len(text)
	// Declare some auxiliary variables
	var sum int = 0
	var num int = 0
	var i int = 0
	// Execute this loop thought by size of text
	for (i < n) {
		// Collect number
		for (i < n && isNumber(text[i])) {
			num = (num * 10) + int(text[i] - '0')
			// Increase index value
			i++
		}
		// Add number
		sum += num
		// Reset the value of num
		num = 0
		// Increase index value
		i++
	}
	fmt.Print(" Given Text : ", text)
	fmt.Println("\n Sum : ", sum)
}
func main() {

	// Test
	sumOfNumbers("u42p2dsf12")
	sumOfNumbers("1gsar2cs")
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
<?php
/*
    Php program for
    Calculate sum of all numbers present in a string
*/
class NumberSum
{
	// Check that if value is number digit or not
	public	function isNumber($value)
	{
		if ($value >= '0' && $value <= '9')
		{
			return true;
		}
		return false;
	}
	public	function sumOfNumbers($text)
	{
		$n = strlen($text);
		// Declare some auxiliary variables
		$sum = 0;
		$num = 0;
		$i = 0;
		// Execute this loop thought by size of text
		while ($i < $n)
		{
			// Collect number
			while ($i < $n && $this->isNumber($text[$i]))
			{
				$num = ($num * 10) + (ord($text[$i]) - ord('0'));
				// Increase index value
				$i++;
			}
			// Add number
			$sum += $num;
			// Reset the value of num
			$num = 0;
			// Increase index value
			$i++;
		}
		echo(" Given Text : ".$text);
		echo("\n Sum : ".$sum.
			"\n");
	}
}

function main()
{
	$task = new NumberSum();
	// Test
	$task->sumOfNumbers("u42p2dsf12");
	$task->sumOfNumbers("1gsar2cs");
}
main();

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
/*
    Node JS program for
    Calculate sum of all numbers present in a string
*/
class NumberSum
{
	// Check that if value is number digit or not
	isNumber(value)
	{
		if (value >= '0' && value <= '9')
		{
			return true;
		}
		return false;
	}
	sumOfNumbers(text)
	{
		var n = text.length;
		// Declare some auxiliary variables
		var sum = 0;
		var num = 0;
		var i = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && this.isNumber(text.charAt(i)))
			{
				num = (num * 10) + 
                  (text.charAt(i).charCodeAt(0) - '0'.charCodeAt(0));
				// Increase index value
				i++;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i++;
		}
		process.stdout.write(" Given Text : " + text);
		console.log("\n Sum : " + sum);
	}
}

function main()
{
	var task = new NumberSum();
	// Test
	task.sumOfNumbers("u42p2dsf12");
	task.sumOfNumbers("1gsar2cs");
}
main();

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
#    Python 3 program for
#    Calculate sum of all numbers present in a string
class NumberSum :
	#  Check that if value is number digit or not
	def isNumber(self, value) :
		if (value >= '0'
			and value <= '9') :
			return True
		
		return False
	
	def sumOfNumbers(self, text) :
		n = len(text)
		#  Declare some auxiliary variables
		sum = 0
		num = 0
		i = 0
		#  Execute this loop thought by size of text
		while (i < n) :
			#  Collect number
			while (i < n and self.isNumber(text[i])) :
				num = (num * 10) + (ord(text[i]) - ord('0'))
				#  Increase index value
				i += 1
			
			#  Add number
			sum += num
			#  Reset the value of num
			num = 0
			#  Increase index value
			i += 1
		
		print(" Given Text : ", text, end = "")
		print("\n Sum : ", sum)
	

def main() :
	task = NumberSum()
	#  Test
	task.sumOfNumbers("u42p2dsf12")
	task.sumOfNumbers("1gsar2cs")

if __name__ == "__main__": main()

Output

 Given Text :  u42p2dsf12
 Sum :  56
 Given Text :  1gsar2cs
 Sum :  3
#    Ruby program for
#    Calculate sum of all numbers present in a string
class NumberSum 
	#  Check that if value is number digit or not
	def isNumber(value) 
		if (value >= '0' && value <= '9') 
			return true
		end

		return false
	end

	def sumOfNumbers(text) 
		n = text.length
		#  Declare some auxiliary variables
		sum = 0
		num = 0
		i = 0
		#  Execute this loop thought by size of text
		while (i < n) 
			#  Collect number
			while (i < n && self.isNumber(text[i])) 
				num = (num * 10) + (text[i].ord - '0'.ord)
				#  Increase index value
				i += 1
			end

			#  Add number
			sum += num
			#  Reset the value of num
			num = 0
			#  Increase index value
			i += 1
		end

		print(" Given Text : ", text)
		print("\n Sum : ", sum, "\n")
	end

end

def main() 
	task = NumberSum.new()
	#  Test
	task.sumOfNumbers("u42p2dsf12")
	task.sumOfNumbers("1gsar2cs")
end

main()

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
/*
    Scala program for
    Calculate sum of all numbers present in a string
*/
class NumberSum()
{
	// Check that if value is number digit or not
	def isNumber(value: Char): Boolean = {
		if (value >= '0' && value <= '9')
		{
			return true;
		}
		return false;
	}
	def sumOfNumbers(text: String): Unit = {
		var n: Int = text.length();
		// Declare some auxiliary variables
		var sum: Int = 0;
		var num: Int = 0;
		var i: Int = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && isNumber(text.charAt(i)))
			{
				num = (num * 10) + (text.charAt(i).toInt - '0'.toInt);
				// Increase index value
				i += 1;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i += 1;
		}
		print(" Given Text : " + text);
		println("\n Sum : " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NumberSum = new NumberSum();
		// Test
		task.sumOfNumbers("u42p2dsf12");
		task.sumOfNumbers("1gsar2cs");
	}
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3
/*
    Swift 4 program for
    Calculate sum of all numbers present in a string
*/
class NumberSum
{
	// Check that if value is number digit or not
	func isNumber(_ value: Character) -> Bool
	{
		if (value >= "0" && value <= "9")
		{
			return true;
		}
		return false;
	}
	func sumOfNumbers(_ data: String)
	{
		let n: Int = data.count;
		// Declare some auxiliary variables
		var sum: Int = 0;
		var num: Int = 0;
		var i: Int = 0;
      	let text = Array(data);
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && self.isNumber(text[i]))
			{
				num = (num * 10) + 
                  (Int(UnicodeScalar(String(text[i]))!.value) -
                   Int(UnicodeScalar(String("0"))!.value));
				// Increase index value
				i += 1;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i += 1;
		}
		print(" Given Text : ", data, terminator: "");
		print("\n Sum : ", sum);
	}
}
func main()
{
	let task: NumberSum = NumberSum();
	// Test
	task.sumOfNumbers("u42p2dsf12");
	task.sumOfNumbers("1gsar2cs");
}
main();

Output

 Given Text :  u42p2dsf12
 Sum :  56
 Given Text :  1gsar2cs
 Sum :  3
/*
    Kotlin program for
    Calculate sum of all numbers present in a string
*/
class NumberSum
{
	// Check that if value is number digit or not
	fun isNumber(value: Char): Boolean
	{
		if (value >= '0' && value <= '9')
		{
			return true;
		}
		return false;
	}
	fun sumOfNumbers(text: String): Unit
	{
		val n: Int = text.length;
		// Declare some auxiliary variables
		var sum: Int = 0;
		var num: Int = 0;
		var i: Int = 0;
		// Execute this loop thought by size of text
		while (i < n)
		{
			// Collect number
			while (i < n && this.isNumber(text.get(i)))
			{
				num = (num * 10) + (text.get(i).toInt() - '0'.toInt());
				// Increase index value
				i += 1;
			}
			// Add number
			sum += num;
			// Reset the value of num
			num = 0;
			// Increase index value
			i += 1;
		}
		print(" Given Text : " + text);
		println("\n Sum : " + sum);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NumberSum = NumberSum();
	// Test
	task.sumOfNumbers("u42p2dsf12");
	task.sumOfNumbers("1gsar2cs");
}

Output

 Given Text : u42p2dsf12
 Sum : 56
 Given Text : 1gsar2cs
 Sum : 3


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







© 2021, kalkicode.com, All rights reserved