Find largest number in string
Given a string which is contain set of number. Our goal is to determine the largest number which is exist in this given string. Note that inside string define all numbers are not exceed the size of number.
Here given code implementation process.
// Java program
// Find largest number in string
class MyString
{
//Finding the max number in given text String
public void max_number(String str)
{
boolean find = false;
//Variable which is storing the information of result
long result = 0;
long auxiliary = 0;
//Loop controlling variables
int i = 0, j = 0, k = 1;
//Loop which is iterate given string character elements
for (i = 0; i < str.length(); i++)
{
if (str.charAt(i) >= '1' && str.charAt(i) <= '9')
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
k = 1;
//When get a number so check that right side element
//When right side is exist found number then group this pair
for (j = i; j < str.length(); j++)
{
//Check valid number between 0-9
if (str.charAt(j) >= '0' && str.charAt(j) <= '9')
{
//When get numbers
auxiliary = (auxiliary * 10) + (str.charAt(j) - '0');
}
else
{
break;
}
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
}
System.out.print(" Given String : [" + str + "]\n Result : ");
if (find == true)
{
System.out.print(result + "\n");
}
else
{
System.out.print("None\n");
}
}
public static void main(String[] args)
{
MyString obj = new MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
}
}
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
// C++ program
// Find largest number in string
//Include header file
#include <iostream>
using namespace std;
class MyString
{
public:
//Finding the max number in given text String
void max_number(string str)
{
bool find = false;
//Variable which is storing the information of result
long result = 0;
long auxiliary = 0;
//Loop controlling variables
int i = 0, j = 0, k = 1;
//Loop which is iterate given string character elements
for (i = 0; i < str.size(); i++)
{
if (str[i] >= '1' && str[i] <= '9')
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
k = 1;
//When get a number so check that right side element
//When right side is exist found number then group this pair
for (j = i; j < str.size(); j++)
{
//Check valid number between 0-9
if (str[j] >= '0' && str[j] <= '9')
{
//When get numbers
auxiliary = (auxiliary * 10) + (str[j] - '0');
}
else
{
break;
}
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
}
cout << " Given String : [" << str << "]\n Result : ";
if (find == true)
{
cout << result << "\n";
}
else
{
cout << "None\n";
}
}
};
int main()
{
MyString obj = MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
return 0;
}
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
// C# program
// Find largest number in string
//Include namespace system
using System;
class MyString
{
//Finding the max number in given text String
public void max_number(String str)
{
Boolean find = false;
//Variable which is storing the information of result
long result = 0;
long auxiliary = 0;
//Loop controlling variables
int i = 0, j = 0;
//Loop which is iterate given string character elements
for (i = 0; i < str.Length; i++)
{
if (str[i] >= '1' && str[i] <= '9')
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
//When get a number so check that right side element
//When right side is exist found number then group this pair
for (j = i; j < str.Length; j++)
{
//Check valid number between 0-9
if (str[j] >= '0' && str[j] <= '9')
{
//When get numbers
auxiliary = (auxiliary * 10) + (str[j] - '0');
}
else
{
break;
}
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
}
Console.Write(" Given String : [" + str + "]\n Result : ");
if (find == true)
{
Console.Write(result + "\n");
}
else
{
Console.Write("None\n");
}
}
public static void Main(String[] args)
{
MyString obj = new MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
}
}
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
<?php
// Php program
// Find largest number in string
class MyString
{
//Finding the max number in given text String
public function max_number($str)
{
$find = false;
//Variable which is storing the information of result
$result = 0;
$auxiliary = 0;
//Loop controlling variables
$i = 0;
$j = 0;
//Loop which is iterate given string character elements
for ($i = 0; $i < strlen($str); $i++)
{
if (ord($str[$i]) >= ord('1') && ord($str[$i]) <= ord('9'))
{
//When get a valid number
$find = true;
//reset variable
$auxiliary = 0;
//When get a number so check that right side element
//When right side is exist found number then group this pair
for ($j = $i; $j < strlen($str); $j++)
{
//Check valid number between 0-9
if (ord($str[$j]) >= ord('0') && ord($str[$j]) <= ord('9'))
{
//When get numbers
$auxiliary = ($auxiliary * 10) + (ord($str[$j]) - ord('0'));
}
else
{
break;
}
}
//move to current calculate position
$i = $j;
if ($auxiliary > $result)
{
//When get a new set of large number
$result = $auxiliary;
}
}
}
echo " Given String : [". $str ."]\n Result : ";
if ($find == true)
{
echo $result ."\n";
}
else
{
echo "None\n";
}
}
}
function main()
{
$obj = new MyString();
// Check Case
$obj->max_number("1k2h3m6b");
$obj->max_number("ihave13ideaofthis0007codes324youare");
$obj->max_number("Iam12-11-2021,check2-1233l");
}
main();
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
// Node Js program
// Find largest number in string
class MyString
{
//Finding the max number in given text String
max_number(str)
{
var find = false;
//Variable which is storing the information of result
var result = 0;
var auxiliary = 0;
//Loop controlling variables
var i = 0;
var j = 0;
//Loop which is iterate given string character elements
for (i = 0; i < str.length; i++)
{
if (str[i] >= '1' && str[i] <= '9')
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
//When get a number so check that right side element
//When right side is exist found number then group this pair
for (j = i; j < str.length; j++)
{
//Check valid number between 0-9
if (str[j] >= '0' && str[j] <= '9')
{
//When get numbers
auxiliary = (auxiliary * 10) + (str[j] - '0');
}
else
{
break;
}
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
}
process.stdout.write(" Given String : [" + str + "]\n Result : ");
if (find == true)
{
process.stdout.write(result + "\n");
}
else
{
process.stdout.write("None\n");
}
}
}
function main()
{
var obj = new MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
}
main();
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
# Python 3 program
# Find largest number in string
class MyString :
# Finding the max number in given text String
def max_number(self, str_data) :
find = False
# Variable which is storing the information of result
result = 0
auxiliary = 0
# Loop controlling variables
i = 0
j = 0
# Loop which is iterate given string character elements
while (i < len(str_data)) :
if (ord(str_data[i]) >= ord('1') and (ord(str_data[i]) <= ord('9'))) :
# When get a valid number
find = True
# reset variable
auxiliary = 0
# When get a number so check that right side element
# When right side is exist found number then group this pair
j = i
while (j < len(str_data)) :
# Check valid number between 0-9
if (ord(str_data[j]) >= ord('0') and ord(str_data[j]) <= ord('9')) :
# When get numbers
auxiliary = (auxiliary * 10) + (ord(str_data[j]) - ord('0'))
else :
break
j += 1
# move to current calculate position
i = j
if (auxiliary > result) :
# When get a new set of large number
result = auxiliary
i += 1
print(" Given String : [", str_data ,"]\n Result : ", end = "")
if (find == True) :
print(result ,"\n", end = "")
else :
print("None\n", end = "")
def main() :
obj = MyString()
# Check Case
obj.max_number("1k2h3m6b")
obj.max_number("ihave13ideaofthis0007codes324youare")
obj.max_number("Iam12-11-2021,check2-1233l")
if __name__ == "__main__": main()
Output
Given String : [ 1k2h3m6b ]
Result : 6
Given String : [ ihave13ideaofthis0007codes324youare ]
Result : 324
Given String : [ Iam12-11-2021,check2-1233l ]
Result : 2021
# Ruby program
# Find largest number in string
class MyString
# Finding the max number in given text String
def max_number(str)
find = false
# Variable which is storing the information of result
result = 0
auxiliary = 0
# Loop controlling variables
i = 0
j = 0
# Loop which is iterate given string character elements
while (i < str.length())
if (str[i].ord >= ('1').ord && str[i].ord <= ('9').ord)
# When get a valid number
find = true
# reset variable
auxiliary = 0
# When get a number so check that right side element
# When right side is exist found number then group this pair
j = i
while (j < str.length())
# Check valid number between 0-9
if (str[j].ord >= ('0').ord && str[j].ord <= ('9').ord)
# When get numbers
auxiliary = (auxiliary * 10) + (str[j].ord - ('0').ord)
else
break
end
j += 1
end
# move to current calculate position
i = j
if (auxiliary > result)
# When get a new set of large number
result = auxiliary
end
end
i += 1
end
print(" Given String : [", str ,"]\n Result : ")
if (find == true)
print(result ,"\n")
else
print("None\n")
end
end
end
def main()
obj = MyString.new()
# Check Case
obj.max_number("1k2h3m6b")
obj.max_number("ihave13ideaofthis0007codes324youare")
obj.max_number("Iam12-11-2021,check2-1233l")
end
main()
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
import scala.util.control.Breaks._
// Scala program
// Find largest number in string
class MyString
{
//Finding the max number in given text String
def max_number(str: String): Unit = {
var find: Boolean = false;
//Variable which is storing the information of result
var result: Long = 0;
var auxiliary: Long = 0;
//Loop controlling variables
var i: Int = 0;
var j: Int = 0;
//Loop which is iterate given string character elements
while (i < str.length())
{
if (str(i) >= '1' && str(i) <= '9')
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
//When get a number so check that right side element
//When right side is exist found number then group this pair
j = i;
breakable {
while (j < str.length())
{
//Check valid number between 0-9
if (str(j) >= '0' && str(j) <= '9')
{
//When get numbers
auxiliary = (auxiliary * 10) + (str(j) - '0');
}
else
{
break;
}
j += 1;
}
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
i += 1;
}
print(" Given String : [" + str + "]\n Result : ");
if (find == true)
{
print(" "+result + "\n");
}
else
{
print("None\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
}
}
Output
Given String : [1k2h3m6b]
Result : 6
Given String : [ihave13ideaofthis0007codes324youare]
Result : 324
Given String : [Iam12-11-2021,check2-1233l]
Result : 2021
// Swift program
// Find largest number in string
class MyString
{
//Finding the max number in given text String
func max_number(_ text: String)
{
var find: Bool = false;
var str = Array(text);
//Variable which is storing the information of result
var result: Int = 0;
var auxiliary: Int = 0;
//Loop controlling variables
var i: Int = 0;
var j: Int = 0;
let zero = Int(UnicodeScalar("0")!.value);
let nine = Int(UnicodeScalar("9")!.value);
//Loop which is iterate given string character elements
while (i < str.count)
{
var char_data = Int(UnicodeScalar(String(str[i]))!.value);
if (char_data >= zero+1 && char_data <= nine)
{
//When get a valid number
find = true;
//reset variable
auxiliary = 0;
//When get a number so check that right side element
//When right side is exist found number then group this pair
j = i;
while (j < text.count)
{
char_data = Int(UnicodeScalar(String(str[j]))!.value);
//Check valid number between 0-9
if (char_data >= zero && char_data <= nine)
{
//When get numbers
auxiliary = (auxiliary * 10) + (char_data - zero);
}
else
{
break;
}
j += 1;
}
//move to current calculate position
i = j;
if (auxiliary > result)
{
//When get a new set of large number
result = auxiliary;
}
}
i += 1;
}
print(" Given String : [", text ,"]\n Result : ", terminator: "");
if (find == true)
{
print(result ,"\n", terminator: "");
}
else
{
print("None\n", terminator: "");
}
}
}
func main()
{
let obj: MyString = MyString();
// Check Case
obj.max_number("1k2h3m6b");
obj.max_number("ihave13ideaofthis0007codes324youare");
obj.max_number("Iam12-11-2021,check2-1233l");
}
main();
Output
Given String : [ 1k2h3m6b ]
Result : 6
Given String : [ ihave13ideaofthis0007codes324youare ]
Result : 324
Given String : [ Iam12-11-2021,check2-1233l ]
Result : 2021
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