Sum of numbers in string
Here given code implementation process.
// Java program
// Sum of all numbers in a string
class MyString
{
// This method are calculate sum of all numbers in given string
public long number_sum(String str_data)
{
long auxiliary = 0;
long result = 0;
//Loop contolling variables
int i = 0, j = 0;
System.out.print("\n String [" + str_data + "] \n Exist Number [");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.length(); i++)
{
if (str_data.charAt(i) >= '0' && str_data.charAt(i) <= '9')
{
//reset variable
auxiliary = 0;
//loop, combine string text
for (j = i; j < str_data.length(); j++)
{
//Check that given character is space character or not
if (str_data.charAt(j) >= '0' && str_data.charAt(j) <= '9')
{
//When not get number
auxiliary = (auxiliary * 10) + (str_data.charAt(j) - '0');
}
else
{
//When get a character of alphabet, break this loop
break;
}
}
if (i > 0 && str_data.charAt(i - 1) == '-')
{
//When number is negative
auxiliary = -auxiliary;
}
System.out.print(" " + auxiliary + " ");
result = result + auxiliary;
i = j;
}
}
System.out.print("]\n");
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
long result = 0;
// Simple case
String text = "11H-mp-Pg23";
result = obj.number_sum(text);
System.out.print(" Number Sum [" + result + "]\n");
text = "hello10iam-7";
result = obj.number_sum(text);
System.out.print(" Number Sum [" + result + "]\n");
}
}
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
//Include header file
#include <iostream>
#include<string.h>
using namespace std;
// C++ program
// Sum of all numbers in a string
class MyString
{
public:
// This method are calculate sum of all numbers in given string
long number_sum(string str_data)
{
long auxiliary = 0;
long result = 0;
//Loop contolling variables
int i = 0, j = 0;
cout << "\n String [" << str_data << "] \n Exist Number [";
//Loop which is iterate given string character elements
for (i = 0; i < str_data.size(); i++)
{
if (str_data[i] >= '0' && str_data[i] <= '9')
{
//reset variable
auxiliary = 0;
//loop, combine string text
for (j = i; j < str_data.size(); j++)
{
//Check that given character is space character or not
if (str_data[j] >= '0' && str_data[j] <= '9')
{
//When not get number
auxiliary = (auxiliary * 10) + (str_data[j] - '0');
}
else
{
//When get a character of alphabet, break this loop
break;
}
}
if (i > 0 && str_data[i - 1] == '-')
{
//When number is negative
auxiliary = -auxiliary;
}
cout << " " << auxiliary << " ";
result = result + auxiliary;
i = j;
}
}
cout << "]\n";
return result;
}
};
int main()
{
MyString obj = MyString();
long result = 0;
// Simple case
string text = "11H-mp-Pg23";
result = obj.number_sum(text);
cout << " Number Sum [" << result << "]\n";
text = "hello10iam-7";
result = obj.number_sum(text);
cout << " Number Sum [" << result << "]\n";
return 0;
}
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
//Include namespace system
using System;
// C# program
// Sum of all numbers in a string
class MyString
{
// This method are calculate sum of all numbers in given string
public long number_sum(String str_data)
{
long auxiliary = 0;
long result = 0;
//Loop contolling variables
int i = 0, j = 0;
Console.Write("\n String [" + str_data + "] \n Exist Number [");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.Length; i++)
{
if (str_data[i] >= '0' && str_data[i] <= '9')
{
//reset variable
auxiliary = 0;
//loop, combine string text
for (j = i; j < str_data.Length; j++)
{
//Check that given character is space character or not
if (str_data[j] >= '0' && str_data[j] <= '9')
{
//When not get number
auxiliary = (auxiliary * 10) + (str_data[j] - '0');
}
else
{
//When get a character of alphabet, break this loop
break;
}
}
if (i > 0 && str_data[i - 1] == '-')
{
//When number is negative
auxiliary = -auxiliary;
}
Console.Write(" " + auxiliary + " ");
result = result + auxiliary;
i = j;
}
}
Console.Write("]\n");
return result;
}
public static void Main(String[] args)
{
MyString obj = new MyString();
long result = 0;
// Simple case
String text = "11H-mp-Pg23";
result = obj.number_sum(text);
Console.Write(" Number Sum [" + result + "]\n");
text = "hello10iam-7";
result = obj.number_sum(text);
Console.Write(" Number Sum [" + result + "]\n");
}
}
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
<?php
// Php program
// Sum of all numbers in a string
class MyString
{
// This method are calculate sum of all numbers in given string
public function number_sum($str_data)
{
$auxiliary = 0;
$result = 0;
//Loop contolling variables
$i = 0;
$j = 0;
echo "\n String [". $str_data ."] \n Exist Number [";
//Loop which is iterate given string character elements
for ($i = 0; $i < strlen($str_data); $i++)
{
if (ord($str_data[$i]) >= ord('0') && ord($str_data[$i]) <= ord('9'))
{
//reset variable
$auxiliary = 0;
//loop, combine string text
for ($j = $i; $j < strlen($str_data); $j++)
{
//Check that given character is space character or not
if (ord($str_data[$j]) >= ord('0') && ord($str_data[$j]) <= ord('9'))
{
//When not get number
$auxiliary = ($auxiliary * 10) + (ord($str_data[$j]) - ord('0'));
}
else
{
//When get a character of alphabet, break this loop
break;
}
}
if ($i > 0 && $str_data[$i - 1] == '-')
{
//When number is negative
$auxiliary = -$auxiliary;
}
echo " ". $auxiliary ." ";
$result = $result + $auxiliary;
$i = $j;
}
}
echo "]\n";
return $result;
}
}
function main()
{
$obj = new MyString();
$result = 0;
// Simple case
$text = "11H-mp-Pg23";
$result = $obj->number_sum($text);
echo " Number Sum [". $result ."]\n";
$text = "hello10iam-7";
$result = $obj->number_sum($text);
echo " Number Sum [". $result ."]\n";
}
main();
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
// Node Js program
// Sum of all numbers in a string
class MyString
{
// This method are calculate sum of all numbers in given string
number_sum(str_data)
{
var auxiliary = 0;
var result = 0;
//Loop contolling variables
var i = 0;
var j = 0;
process.stdout.write("\n String [" + str_data + "] \n Exist Number [");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.length; i++)
{
if (str_data[i] >= '0' && str_data[i] <= '9')
{
//reset variable
auxiliary = 0;
//loop, combine string text
for (j = i; j < str_data.length; j++)
{
//Check that given character is space character or not
if (str_data[j] >= '0' && str_data[j] <= '9')
{
//When not get number
auxiliary = (auxiliary * 10) + (str_data[j] - '0');
}
else
{
//When get a character of alphabet, break this loop
break;
}
}
if (i > 0 && str_data[i - 1] == '-')
{
//When number is negative
auxiliary = -auxiliary;
}
process.stdout.write(" " + auxiliary + " ");
result = result + auxiliary;
i = j;
}
}
process.stdout.write("]\n");
return result;
}
}
function main()
{
var obj = new MyString();
var result = 0;
// Simple case
var text = "11H-mp-Pg23";
result = obj.number_sum(text);
process.stdout.write(" Number Sum [" + result + "]\n");
text = "hello10iam-7";
result = obj.number_sum(text);
process.stdout.write(" Number Sum [" + result + "]\n");
}
main();
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
# Python 3 program
# Sum of all numbers in a string
class MyString :
# This method are calculate sum of all numbers in given string
def number_sum(self, str_data) :
auxiliary = 0
result = 0
# Loop contolling variables
i = 0
j = 0
print("\n String [", str_data ,"] \n Exist Number [", end = "")
# Loop which is iterate given string character elements
while (i < len(str_data)) :
if (ord(str_data[i]) >= ord('0') and ord(str_data[i]) <= ord('9')) :
# reset variable
auxiliary = 0
# loop, combine string text
j = i
while (j < len(str_data)) :
# Check that given character is space character or not
if (ord(str_data[j]) >= ord('0') and ord(str_data[j]) <= ord('9')) :
# When not get number
auxiliary = (auxiliary * 10) + (ord(str_data[j]) - ord('0'))
else :
# When get a character of alphabet, break this loop
break
j += 1
if (i > 0 and str_data[i - 1] == '-') :
# When number is negative
auxiliary = -auxiliary
print(" ", auxiliary ," ", end = "")
result = result + auxiliary
i = j
i += 1
print("]\n", end = "")
return result
def main() :
obj = MyString()
result = 0
# Simple case
text = "11H-mp-Pg23"
result = obj.number_sum(text)
print(" Number Sum [", result ,"]\n", end = "")
text = "hello10iam-7"
result = obj.number_sum(text)
print(" Number Sum [", result ,"]\n", end = "")
if __name__ == "__main__": main()
Output
String [ 11H-mp-Pg23 ]
Exist Number [ 11 23 ]
Number Sum [ 34 ]
String [ hello10iam-7 ]
Exist Number [ 10 -7 ]
Number Sum [ 3 ]
# Ruby program
# Sum of all numbers in a string
class MyString
# This method are calculate sum of all numbers in given string
def number_sum(str_data)
auxiliary = 0
result = 0
# Loop contolling variables
i = 0
j = 0
print("\n String [", str_data ,"] \n Exist Number [")
# Loop which is iterate given string character elements
while (i < str_data.length())
if (str_data[i].ord >= ('0').ord && str_data[i].ord <= ('9').ord)
# reset variable
auxiliary = 0
# loop, combine string text
j = i
while (j < str_data.length())
# Check that given character is space character or not
if (str_data[j].ord >= ('0').ord && str_data[j].ord <= ('9').ord)
# When not get number
auxiliary = (auxiliary * 10) + (str_data[j].ord - ('0').ord)
else
# When get a character of alphabet, break this loop
break
end
j += 1
end
if (i > 0 && str_data[i - 1] == '-')
# When number is negative
auxiliary = -auxiliary
end
print(" ", auxiliary ," ")
result = result + auxiliary
i = j
end
i += 1
end
print("]\n")
return result
end
end
def main()
obj = MyString.new()
result = 0
# Simple case
text = "11H-mp-Pg23"
result = obj.number_sum(text)
print(" Number Sum [", result ,"]\n")
text = "hello10iam-7"
result = obj.number_sum(text)
print(" Number Sum [", result ,"]\n")
end
main()
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
// Scala program
// Sum of all numbers in a string
//for break statement
import scala.util.control.Breaks._
class MyString
{
// This method are calculate sum of all numbers in given string
def number_sum(str_data: String): Long = {
var auxiliary: Long = 0;
var result: Long = 0;
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\n String [" + str_data + "] \n Exist Number [");
//Loop which is iterate given string character elements
while (i < str_data.length())
{
if (str_data(i) >= '0' && str_data(i) <= '9')
{
//reset variable
auxiliary = 0;
//loop, combine string text
j = i;
breakable {
while (j < str_data.length())
{
//Check that given character is space character or not
if (str_data(j) >= '0' && str_data(j) <= '9')
{
//When not get number
auxiliary = (auxiliary * 10) + (str_data(j) - '0');
}
else
{
//When get a character of alphabet, break this loop
break;
}
j += 1;
}
}
if (i > 0 && str_data(i - 1) == '-')
{
//When number is negative
auxiliary = -auxiliary;
}
print(" " + auxiliary + " ");
result = result + auxiliary;
i = j;
}
i += 1;
}
print("]\n");
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var result: Long = 0;
// Simple case
var text: String = "11H-mp-Pg23";
result = obj.number_sum(text);
print(" Number Sum [" + result + "]\n");
text = "hello10iam-7";
result = obj.number_sum(text);
print(" Number Sum [" + result + "]\n");
}
}
Output
String [11H-mp-Pg23]
Exist Number [ 11 23 ]
Number Sum [34]
String [hello10iam-7]
Exist Number [ 10 -7 ]
Number Sum [3]
// Swift program
// Sum of all numbers in a string
class MyString
{
// This method are calculate sum of all numbers in given string
func number_sum(_ text: String) -> Int
{
let str_data = Array(text);
var auxiliary: Int = 0;
var result: Int = 0;
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\n String [", text ,"] \n Exist Number [", terminator: "");
let zero = Int(UnicodeScalar("0")!.value);
let nine = Int(UnicodeScalar("9")!.value);
//Loop which is iterate given string character elements
while (i < str_data.count)
{
var char_data = Int(UnicodeScalar(String(str_data[i]))!.value);
if (char_data >= zero+1 && char_data <= nine)
{
//reset variable
auxiliary = 0;
//loop, combine string text
j = i;
while (j < str_data.count)
{
char_data = Int(UnicodeScalar(String(str_data[j]))!.value);
//Check that given character is space character or not
if (char_data >= zero && char_data <= nine)
{
//When not get number
auxiliary = (auxiliary * 10) + (char_data - zero);
}
else
{
//When get a character of alphabet, break this loop
break;
}
j += 1;
}
if (i > 0 && str_data[i - 1] == "-")
{
//When number is negative
auxiliary = -auxiliary;
}
print(" ", auxiliary ," ", terminator: "");
result = result + auxiliary;
i = j;
}
i += 1;
}
print("]\n", terminator: "");
return result;
}
}
func main()
{
let obj: MyString = MyString();
var result: Int = 0;
// Simple case
var text: String = "11H-mp-Pg23";
result = obj.number_sum(text);
print(" Number Sum [", result ,"]\n", terminator: "");
text = "hello10iam-7";
result = obj.number_sum(text);
print(" Number Sum [", result ,"]\n", terminator: "");
}
main();
Output
String [ 11H-mp-Pg23 ]
Exist Number [ 11 23 ]
Number Sum [ 34 ]
String [ hello10iam-7 ]
Exist Number [ 10 -7 ]
Number 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