Check if string contains only digits

Here given code implementation process.

// C Program for
// Check if string contains only digits
#include <stdio.h>
#include <string.h>

void isOnlyDigit(char *text)
{
	int n = strlen(text);
	if (n == 0)
	{
		// Empty String
		return;
	}
	printf("\n Given text  : %s ", text);
	// Check given number contain digit or not
	for (int i = 0; i < n; ++i)
	{
		if (!(text[i] >= '0' && text[i] <= '9'))
		{
			// When character is not digits 
			printf("\n  No ");
			return;
		}
	}
	// When contain digits 
	printf("\n  Yes ");
}
int main(int argc, char const *argv[])
{
	// Test
	isOnlyDigit("3423424");
	isOnlyDigit("-32423423423452354234");
	isOnlyDigit("945324s574835");
	isOnlyDigit("908");
	return 0;
}

Output

 Given text  : 3423424
  Yes
 Given text  : -32423423423452354234
  No
 Given text  : 945324s574835
  No
 Given text  : 908
  Yes
// Java program for
// Check if string contains only digits
public class DigitCheck
{
	public void isOnlyDigit(String text)
	{
		int n = text.length();
		if (n == 0)
		{
			// Empty String
			return;
		}
		System.out.print("\n Given text : " + text);
		// Check given number contain digit or not
		for (int i = 0; i < n; ++i)
		{
			if (!(text.charAt(i) >= '0' && text.charAt(i) <= '9'))
			{
				// When character is not digits 
				System.out.print("\n No ");
				return;
			}
		}
		// When contain digits 
		System.out.print("\n Yes ");
	}
	public static void main(String[] args)
	{
		DigitCheck task = new DigitCheck();
		// Test
		task.isOnlyDigit("3423424");
		task.isOnlyDigit("-32423423423452354234");
		task.isOnlyDigit("945324s574835");
		task.isOnlyDigit("908");
	}
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
package main
import "fmt"
// Go program for
// Check if string contains only digits
type DigitCheck struct {}
func getDigitCheck() * DigitCheck {
	var me *DigitCheck = &DigitCheck {}
	return me
}
func(this DigitCheck) isOnlyDigit(text string) {
	var n int = len(text)
	if n == 0 {
		// Empty String
		return
	}
	fmt.Print("\n Given text : ", text)
	// Check given number contain digit or not
	for i := 0 ; i < n ; i++ {
		if !(text[i] >= '0' && text[i] <= '9') {
			// When character is not digits 
			fmt.Print("\n No ")
			return
		}
	}
	// When contain digits 
	fmt.Print("\n Yes ")
}
func main() {
	var task * DigitCheck = getDigitCheck()
	// Test
	task.isOnlyDigit("3423424")
	task.isOnlyDigit("-32423423423452354234")
	task.isOnlyDigit("945324s574835")
	task.isOnlyDigit("908")
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check if string contains only digits
class DigitCheck
{
	public: void isOnlyDigit(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			// Empty String
			return;
		}
		cout << "\n Given text : " << text;
		// Check given number contain digit or not
		for (int i = 0; i < n; ++i)
		{
			if (!(text[i] >= '0' && text[i] <= '9'))
			{
				// When character is not digits 
				cout << "\n No ";
				return;
			}
		}
		// When contain digits 
		cout << "\n Yes ";
	}
};
int main()
{
	DigitCheck *task = new DigitCheck();
	// Test
	task->isOnlyDigit("3423424");
	task->isOnlyDigit("-32423423423452354234");
	task->isOnlyDigit("945324s574835");
	task->isOnlyDigit("908");
	return 0;
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
// Include namespace system
using System;
// Csharp program for
// Check if string contains only digits
public class DigitCheck
{
	public void isOnlyDigit(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			// Empty String
			return;
		}
		Console.Write("\n Given text : " + text);
		// Check given number contain digit or not
		for (int i = 0; i < n; ++i)
		{
			if (!(text[i] >= '0' && text[i] <= '9'))
			{
				// When character is not digits 
				Console.Write("\n No ");
				return;
			}
		}
		// When contain digits 
		Console.Write("\n Yes ");
	}
	public static void Main(String[] args)
	{
		DigitCheck task = new DigitCheck();
		// Test
		task.isOnlyDigit("3423424");
		task.isOnlyDigit("-32423423423452354234");
		task.isOnlyDigit("945324s574835");
		task.isOnlyDigit("908");
	}
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
<?php
// Php program for
// Check if string contains only digits
class DigitCheck
{
	public	function isOnlyDigit($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			// Empty String
			return;
		}
		echo("\n Given text : ".$text);
		// Check given number contain digit or not
		for ($i = 0; $i < $n; ++$i)
		{
			if (!($text[$i] >= '0' && $text[$i] <= '9'))
			{
				// When character is not digits 
				echo("\n No ");
				return;
			}
		}
		// When contain digits 
		echo("\n Yes ");
	}
}

function main()
{
	$task = new DigitCheck();
	// Test
	$task->isOnlyDigit("3423424");
	$task->isOnlyDigit("-32423423423452354234");
	$task->isOnlyDigit("945324s574835");
	$task->isOnlyDigit("908");
}
main();

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
// Node JS program for
// Check if string contains only digits
class DigitCheck
{
	isOnlyDigit(text)
	{
		var n = text.length;
		if (n == 0)
		{
			// Empty String
			return;
		}
		process.stdout.write("\n Given text : " + text);
		// Check given number contain digit or not
		for (var i = 0; i < n; ++i)
		{
			if (!(text.charAt(i) >= '0' && text.charAt(i) <= '9'))
			{
				// When character is not digits 
				process.stdout.write("\n No ");
				return;
			}
		}
		// When contain digits 
		process.stdout.write("\n Yes ");
	}
}

function main()
{
	var task = new DigitCheck();
	// Test
	task.isOnlyDigit("3423424");
	task.isOnlyDigit("-32423423423452354234");
	task.isOnlyDigit("945324s574835");
	task.isOnlyDigit("908");
}
main();

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
#  Python 3 program for
#  Check if string contains only digits
class DigitCheck :
	def isOnlyDigit(self, text) :
		n = len(text)
		if (n == 0) :
			#  Empty String
			return
		
		print("\n Given text : ", text, end = "")
		i = 0
		#  Check given number contain digit or not
		while (i < n) :
			if (not(text[i] >= '0'
					and text[i] <= '9')) :
				#  When character is not digits 
				print("\n No ", end = "")
				return
			
			i += 1
		
		#  When contain digits 
		print("\n Yes ", end = "")
	

def main() :
	task = DigitCheck()
	#  Test
	task.isOnlyDigit("3423424")
	task.isOnlyDigit("-32423423423452354234")
	task.isOnlyDigit("945324s574835")
	task.isOnlyDigit("908")

if __name__ == "__main__": main()

Output

 Given text :  3423424
 Yes
 Given text :  -32423423423452354234
 No
 Given text :  945324s574835
 No
 Given text :  908
 Yes
#  Ruby program for
#  Check if string contains only digits
class DigitCheck 
	def isOnlyDigit(text) 
		n = text.length
		if (n == 0) 
			#  Empty String
			return
		end

		print("\n Given text : ", text)
		i = 0
		#  Check given number contain digit or not
		while (i < n) 
			if (!(text[i] >= '0' && text[i] <= '9')) 
				#  When character is not digits 
				print("\n No ")
				return
			end

			i += 1
		end

		#  When contain digits 
		print("\n Yes ")
	end

end

def main() 
	task = DigitCheck.new()
	#  Test
	task.isOnlyDigit("3423424")
	task.isOnlyDigit("-32423423423452354234")
	task.isOnlyDigit("945324s574835")
	task.isOnlyDigit("908")
end

main()

Output

 Given text : 3423424
 Yes 
 Given text : -32423423423452354234
 No 
 Given text : 945324s574835
 No 
 Given text : 908
 Yes 
import scala.collection.mutable._;
// Scala program for
// Check if string contains only digits
class DigitCheck()
{
	def isOnlyDigit(text: String): Unit = {
		var n: Int = text.length();
		if (n == 0)
		{
			// Empty String
			return;
		}
		print("\n Given text : " + text);
		var i: Int = 0;
		// Check given number contain digit or not
		while (i < n)
		{
			if (!(text.charAt(i) >= '0' && text.charAt(i) <= '9'))
			{
				// When character is not digits 
				print("\n No ");
				return;
			}
			i += 1;
		}
		// When contain digits 
		print("\n Yes ");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DigitCheck = new DigitCheck();
		// Test
		task.isOnlyDigit("3423424");
		task.isOnlyDigit("-32423423423452354234");
		task.isOnlyDigit("945324s574835");
		task.isOnlyDigit("908");
	}
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes
import Foundation;
// Swift 4 program for
// Check if string contains only digits
class DigitCheck
{
	func isOnlyDigit(_ data: String)
	{
      	let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			// Empty String
			return;
		}
		print("\n Given text : ", data, terminator: "");
		var i: Int = 0;
		// Check given number contain digit or not
		while (i < n)
		{
			if (!(text[i] >= "0" && text[i] <= "9"))
			{
				// When character is not digits 
				print("\n No ", terminator: "");
				return;
			}
			i += 1;
		}
		// When contain digits 
		print("\n Yes ", terminator: "");
	}
}
func main()
{
	let task: DigitCheck = DigitCheck();
	// Test
	task.isOnlyDigit("3423424");
	task.isOnlyDigit("-32423423423452354234");
	task.isOnlyDigit("945324s574835");
	task.isOnlyDigit("908");
}
main();

Output

 Given text :  3423424
 Yes
 Given text :  -32423423423452354234
 No
 Given text :  945324s574835
 No
 Given text :  908
 Yes
// Kotlin program for
// Check if string contains only digits
class DigitCheck
{
	fun isOnlyDigit(text: String): Unit
	{
		val n: Int = text.length;
		if (n == 0)
		{
			// Empty String
			return;
		}
		print("\n Given text : " + text);
		var i: Int = 0;
		// Check given number contain digit or not
		while (i < n)
		{
			if (!(text.get(i) >= '0' && text.get(i) <= '9'))
			{
				// When character is not digits 
				print("\n No ");
				return;
			}
			i += 1;
		}
		// When contain digits 
		print("\n Yes ");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DigitCheck = DigitCheck();
	// Test
	task.isOnlyDigit("3423424");
	task.isOnlyDigit("-32423423423452354234");
	task.isOnlyDigit("945324s574835");
	task.isOnlyDigit("908");
}

Output

 Given text : 3423424
 Yes
 Given text : -32423423423452354234
 No
 Given text : 945324s574835
 No
 Given text : 908
 Yes


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