Check if a string contains at least one Number

Problem statement:- Program to Check if a string contains at least one Number(digit) or not.

Example:-

              Input: Given

                                 String=csinfo

             Output: String does not contain at least one digits.

            Input: Given

                                String=csinfo360

             Output: String contains at least one digits.

Data requirement:-


   Input Data:- str

  Output Data:-
string output

  Additional Data:- 
in, count, len

Program in C

Here is the source code of the C Program to Check if a string contains at least one Number(digit) or not.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    int count=0,in;
    char str[100]={0};
    printf("Enter your String:");
    gets(str);

     for(in=0;in<strlen(str);in++)
    {
            if(str[in] >= '0' && str[in] <= '9')
            {
                  count++;
            }
    }
    if(count>=1)
        printf("String contains at least one digits.");
    else
        printf("String does not contain at least one digits.");
}

Input/Output:
Enter your String:csinfo
String does not contain at least one digits.

Program in C++

Here is the source code of the C++ Program to Check if a string contains at least one Number(digit) or not.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    int count=0,in;
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0;
    for(in=0;str[in]!='\0';in++)
    {
        len++;
    }
     for(in=0;in<len;in++)
    {
            if(str[in] >= '0' && str[in] <= '9')
            {
                  count++;
            }
    }
   if(count>=1)
        cout<<"String contains at least one digits.";
    else
        cout<<"String does not contains any digits.";
}

Input/Output:
Enter your String:csinfo360
String contains at least one digits.

Program in Java

Here is the source code of the Java Program to Check if a string contains at least one Number(digit) or not.

Code:

import java.util.Scanner;
public class AtLeastOneDigits {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
int count=0,in;
System.out.println("Enter your String:");
str=cs.nextLine();
for(in=0;in<str.length();in++)
    {
            if(str.charAt(in) >= '0' && str.charAt(in) <= '9')
            {
                  count++;
            }
    }
if(count>=1)
System.out.print("String contains at least one digits.");
    else
    System.out.print("String does not contain at least one digits.");
cs.close();
}
}

Input/Output:
Enter your String:
csinfo360.com
String contains at least one digits.

Program in Python

Here is the source code of the Python Program to Check if a string contains at least one Number(digit) or not.

Code:

str=input("Enter Your String:")
count=0
for inn in range(0,len(str)):
    if str[inn] >= '0' and  str[inn] <= '9':
        count+=1
if count>=1:
    print("String contains at least one digits.")
else:
    print("String does not contains at least one digits.")

Input/Output:
Enter Your String:python string question
String does not contain at least one digits.

Post a Comment

0 Comments