Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Program to construct a DFA which accepts the language L = {aN | N ≥ 1}
Next article icon

Program to construct a DFA to check if a given integer is unsigned or not

Last Updated : 20 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string S that represents an integer, the task is to check if the given string S represents an unsigned integer or not by constructing the DFA. If the given string represents an unsigned integer, then print "Unsigned integer". Otherwise, print "Not an unsigned integer".

Examples:

Input: S = "+4554"
Output: Not an unsigned integer

Input: S = "1729"
Output: unsigned integer

Approach: Below is the transition states of DFA for the given problem:

Follow the steps below to solve the problem:

  • Declare a function to build and connect DFA states as per the given conditions. Below are the representation of the transitions states:
    • 0 represents a digit.
    • 1 represents sign "+/-".
    • 2 represents "." or dot.
    • 3 represents any other character.
    • 4 represents exponent(e/E sign).
  • Initialize a variable, say currentState as 0, that stores the current state in the DFA.
  • Traverse the string S and based on the current state and present character at the current index decide the next state of the DFA.
  • After completing the above steps, if the value of currentState is either 1 or 4 or 8, then print "Unsigned integer". Otherwise, print "Not an unsigned integer".

Below is the implementation of the above approach:

C++
// C++ program for the above approach  #include "bits/stdc++.h" using namespace std;  string digits = "0123456789", sign = "+-"; string dot = ".", ex = "eE"; int dfa[11][5];  // Function to construct DFA as per the // given conditions void makeDFA() {     // If at state 0 and a digit has     // occurred then set it to state 1     dfa[0][0] = 1;      // Similarly for all other states     dfa[1][0] = 1;     dfa[1][2] = 3;     dfa[1][3] = 2;     dfa[1][4] = 6;      dfa[3][0] = 4;      dfa[4][0] = 4;     dfa[4][3] = 5;     dfa[4][4] = 6;      dfa[6][0] = 8;     dfa[6][1] = 7;      dfa[7][0] = 8;      dfa[8][0] = 8;     dfa[8][3] = 9; }  // Function to build and connect // the DFA states void buildDFA() {     // Connect all the states to the     // dead state     for (int i = 0; i < 11; i++)         for (int j = 0; j < 5; j++)             dfa[i][j] = 10;      // Function call to make DFA as     // per the given conditions     makeDFA(); }  // Function call to check whether an // integer in the form of string is // unsigned integer or not void checkDFA(string s) {     // Build the DFA     buildDFA();      // Stores the current state     int currentstate = 0;      // Traverse the string     for (int i = 0; i < s.size(); i++) {          if (digits.find(s[i])             != digits.npos)              // If at a certain state a             // digit occurs then change             // the current state according             // to the DFA             currentstate                 = dfa[currentstate][0];          // Or +/- sign         else if (sign.find(s[i])                  != sign.npos)             currentstate                 = dfa[currentstate][1];          // Or decimal occurred         else if (dot.find(s[i])                  != dot.npos)             currentstate                 = dfa[currentstate][2];          // Or any other character         else if (ex.find(s[i])                  != ex.npos)             currentstate                 = dfa[currentstate][4];          // Or e/E or exponent sign         else             currentstate                 = dfa[currentstate][3];     }      // State 1, 4, 8 will give     // the final answer     if (currentstate == 1         || currentstate == 4         || currentstate == 8) {         cout << "Unsigned integer";     }     else {         cout << "Not an unsigned integer";     } }  // Driver Code int main() {     string S = "1729";     checkDFA(S);      return 0; } 
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*;  class GFG{  static String digits = "0123456789", sign = "+-"; static String dot = ".", ex = "eE"; static int dfa[][] = new int[11][5];  // Function to construct DFA as per the // given conditions static void makeDFA() {          // If at state 0 and a digit has     // occurred then set it to state 1     dfa[0][0] = 1;      // Similarly for all other states     dfa[1][0] = 1;     dfa[1][2] = 3;     dfa[1][3] = 2;     dfa[1][4] = 6;      dfa[3][0] = 4;      dfa[4][0] = 4;     dfa[4][3] = 5;     dfa[4][4] = 6;      dfa[6][0] = 8;     dfa[6][1] = 7;      dfa[7][0] = 8;      dfa[8][0] = 8;     dfa[8][3] = 9; }  // Function to build and connect // the DFA states static void buildDFA() {          // Connect all the states to the     // dead state     for(int i = 0; i < 11; i++)         for(int j = 0; j < 5; j++)             dfa[i][j] = 10;      // Function call to make DFA as     // per the given conditions     makeDFA(); }  // Function call to check whether an // integer in the form of string is // unsigned integer or not static void checkDFA(String s) {          // Build the DFA     buildDFA();      // Stores the current state     int currentstate = 0;      // Traverse the string     for(int i = 0; i < s.length(); i++)     {         if (digits.indexOf(s.charAt(i)) != -1)              // If at a certain state a             // digit occurs then change             // the current state according             // to the DFA             currentstate = dfa[currentstate][0];          // Or +/- sign         else if (sign.indexOf(s.charAt(i)) != -1)             currentstate = dfa[currentstate][1];          // Or decimal occurred         else if (dot.indexOf(s.charAt(i)) != -1)             currentstate = dfa[currentstate][2];          // Or any other character         else if (ex.indexOf(s.charAt(i)) != -1)             currentstate = dfa[currentstate][4];          // Or e/E or exponent sign         else             currentstate = dfa[currentstate][3];     }      // State 1, 4, 8 will give     // the final answer     if (currentstate == 1 || currentstate == 4 ||          currentstate == 8)      {         System.out.println("Unsigned integer");     }     else      {         System.out.println("Not an unsigned integer");     } }  // Driver Code public static void main(String[] args) {     String S = "1729";          checkDFA(S); } }  // This code is contributed by Kingash 
Python3
# Python3 program for the above approach digits,sign = "0123456789", "+-" dot, ex = ".", "eE" dfa = [[0 for i in range(5)] for i in range(11)]  # Function to construct DFA as per the # given conditions def makeDFA():     global dfa      # If at state 0 and a digit has     # occurred then set it to state 1     dfa[0][0] = 1      # Similarly for all other states     dfa[1][0] = 1     dfa[1][2] = 3     dfa[1][3] = 2     dfa[1][4] = 6      dfa[3][0] = 4      dfa[4][0] = 4     dfa[4][3] = 5     dfa[4][4] = 6      dfa[6][0] = 8     dfa[6][1] = 7      dfa[7][0] = 8      dfa[8][0] = 8     dfa[8][3] = 9   # Function to build and connect # the DFA states def buildDFA():     global dfa     # Connect all the states to the     # dead state     for i in range(11):         for j in range(5):             dfa[i][j] = 10      # Function call to make DFA as     # per the given conditions     makeDFA()  # Function call to check whether an # integer in the form of is # unsigned integer or not def checkDFA(s):     # Build the DFA     buildDFA()      # Stores the current state     currentstate = 0      # Traverse the string     for i in range(len(s)):          if (s[i] in digits):              # If at a certain state a             # digit occurs then change             # the current state according             # to the DFA             currentstate = dfa[currentstate][0]          # Or +/- sign         elif (s[i] in sign):             currentstate = dfa[currentstate][1]          # Or decimal occurred         elif (s[i] in dot):             currentstate = dfa[currentstate][2]          # Or any other character         elif (s[i] in ex):             currentstate = dfa[currentstate][4]          # Or e/E or exponent sign         else:             currentstate = dfa[currentstate][3]      # State 1, 4, 8 will give     # the final answer     if (currentstate == 1 or currentstate == 4 or currentstate == 8):         print("Unsigned integer")     else:         print("Not an unsigned integer")  # Driver Code if __name__ == '__main__':     S = "1729"     checkDFA(S)      # This code is contributed by mohit kumar 29. 
C#
// C# program for the above approach using System;  class GFG{  static string digits = "0123456789", sign = "+-"; static string dot = ".", ex = "eE"; static int[,] dfa = new int[11, 5];  // Function to construct DFA as per the // given conditions static void makeDFA() {      // If at state 0 and a digit has     // occurred then set it to state 1     dfa[0, 0] = 1;      // Similarly for all other states     dfa[1, 0] = 1;     dfa[1, 2] = 3;     dfa[1, 3] = 2;     dfa[1, 4] = 6;      dfa[3, 0] = 4;      dfa[4, 0] = 4;     dfa[4, 3] = 5;     dfa[4, 4] = 6;      dfa[6, 0] = 8;     dfa[6, 1] = 7;      dfa[7, 0] = 8;      dfa[8, 0] = 8;     dfa[8, 3] = 9; }  // Function to build and connect // the DFA states static void buildDFA() {      // Connect all the states to the     // dead state     for(int i = 0; i < 11; i++)         for(int j = 0; j < 5; j++)             dfa[i, j] = 10;      // Function call to make DFA as     // per the given conditions     makeDFA(); }  // Function call to check whether an // integer in the form of string is // unsigned integer or not static void checkDFA(string s) {      // Build the DFA     buildDFA();      // Stores the current state     int currentstate = 0;      // Traverse the string     for(int i = 0; i < s.Length; i++)      {         if (digits.IndexOf(s[i]) != -1)              // If at a certain state a             // digit occurs then change             // the current state according             // to the DFA             currentstate = dfa[currentstate, 0];          // Or +/- sign         else if (sign.IndexOf(s[i]) != -1)             currentstate = dfa[currentstate, 1];          // Or decimal occurred         else if (dot.IndexOf(s[i]) != -1)             currentstate = dfa[currentstate, 2];          // Or any other character         else if (ex.IndexOf(s[i]) != -1)             currentstate = dfa[currentstate, 4];          // Or e/E or exponent sign         else             currentstate = dfa[currentstate, 3];     }      // State 1, 4, 8 will give     // the final answer     if (currentstate == 1 || currentstate == 4 ||          currentstate == 8)     {         Console.WriteLine("Unsigned integer");     }     else      {         Console.WriteLine("Not an unsigned integer");     } }  // Driver Code public static void Main(string[] args) {     string S = "1729";      checkDFA(S); } }  // This code is contributed by ukasp 
JavaScript
<script>  // JavaScript program for the above approach  let digits = "0123456789", sign = "+-"; let dot = ".", ex = "eE";  let dfa = new Array(11); for(let i=0;i<11;i++) {     dfa[i]=new Array(5);      }  // Function to construct DFA as per the // given conditions function makeDFA() {     // If at state 0 and a digit has     // occurred then set it to state 1     dfa[0][0] = 1;       // Similarly for all other states     dfa[1][0] = 1;     dfa[1][2] = 3;     dfa[1][3] = 2;     dfa[1][4] = 6;       dfa[3][0] = 4;       dfa[4][0] = 4;     dfa[4][3] = 5;     dfa[4][4] = 6;       dfa[6][0] = 8;     dfa[6][1] = 7;       dfa[7][0] = 8;       dfa[8][0] = 8;     dfa[8][3] = 9; }  // Function to build and connect // the DFA states function buildDFA() {     // Connect all the states to the     // dead state     for(let i = 0; i < 11; i++)         for(let j = 0; j < 5; j++)             dfa[i][j] = 10;       // Function call to make DFA as     // per the given conditions     makeDFA(); }  // Function call to check whether an // integer in the form of string is // unsigned integer or not function  checkDFA(s) {     // Build the DFA     buildDFA();       // Stores the current state     let currentstate = 0;       // Traverse the string     for(let i = 0; i < s.length; i++)     {         if (digits.indexOf(s[i]) != -1)               // If at a certain state a             // digit occurs then change             // the current state according             // to the DFA             currentstate = dfa[currentstate][0];           // Or +/- sign         else if (sign.indexOf(s[i]) != -1)             currentstate = dfa[currentstate][1];           // Or decimal occurred         else if (dot.indexOf(s[i]) != -1)             currentstate = dfa[currentstate][2];           // Or any other character         else if (ex.indexOf(s[i]) != -1)             currentstate = dfa[currentstate][4];           // Or e/E or exponent sign         else             currentstate = dfa[currentstate][3];     }       // State 1, 4, 8 will give     // the final answer     if (currentstate == 1 || currentstate == 4 ||         currentstate == 8)     {         document.write("Unsigned integer<br>");     }     else     {         document.write("Not an unsigned integer");     } } // Driver Code let S = "1729";       checkDFA(S);   // This code is contributed by avanitrachhadiya2155  </script> 

Output: 
Unsigned integer

 

Time Complexity: O(N)
Auxiliary Space: O(1),  since no extra space has been taken.


Next Article
Program to construct a DFA which accepts the language L = {aN | N ≥ 1}
author
abhijeet010304
Improve
Article Tags :
  • Strings
  • Compiler Design
  • Theory of Computation
  • DSA
  • DFA
Practice Tags :
  • Strings

Similar Reads

  • Program to check if input is an integer or a string
    Write a function to check whether a given input is an integer or a string. Definition of an integer : Every element should be a valid digit, i.e '0-9'. Definition of a string : Any one element should be an invalid digit, i.e any symbol other than '0-9'. Examples: Input : 127Output : IntegerExplanati
    15+ min read
  • Program to construct a DFA which accepts the language L = {aN | N ≥ 1}
    Prerequisite: Finite Automata Given a string S of size N, the task is to design a Deterministic Finite Automata (DFA) for accepting the language L = {aN | N ? 1}. The regular language L is {a, aa, aaa, aaaaaaa..., }. If the given string follows the given language L, then print "Accepted". Otherwise,
    5 min read
  • Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
    Validate if a given string is numeric. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 10e5.4 Output : false The following cases need to be handled in the code. Ignore the leading and trailing white spaces.Ignore the '+', '-'
    11 min read
  • Lex program to check whether the input is digit or not
    Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator). Given an input, the task is to check if the input
    1 min read
  • Check if an integer is rotation of another given integer
    Given two integers A and B, the task is to check if the integer A is rotation of the digits of the integer B or not. If found to be true, then print "Yes". Otherwise print "No". Examples: Input: A= 976, B= 679Output: Yes Input: A= 974, B= 2345Output: No Recommended: Please try your approach on {IDE}
    9 min read
  • Find whether a given integer is a power of 3 or not
    Given a positive integer, write a function to find if it is a power of three or not. Examples: Input : 3Output :YesInput :6Output :NoGeneral Solution to check if any number N is a power of P: Refer to the code below for implementation. [GFGTABS] C++ #include <iostream> using namespace std; #in
    15+ min read
  • Check if a number is in given base or not
    Given a number as a string and a base, check if the given number is in the given base or not. Examples: Input : str = "1010", base = 2 Output : Yes Input : str = "1015", base = 2 Output : No Input : str = "AF87", base = 16 Output : Yes The idea is to one by one check if all digits are in the given b
    10 min read
  • Program to check if a number is divisible by any of its digits
    Given an integer N where [Tex]1 \leq n \leq 10^{18} [/Tex]. The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by
    10 min read
  • Program to check if a number is divisible by sum of its digits
    Given an integer N, the task is to check whether the number is divisible by the sum of its digits or not. If divisible, then print “YES” else print “NO”. Examples: Input: N = 12 Output: YES Explanation: As sum of digits of 12 = 1 + 2 = 3 and 12 is divisible by 3 So the output is YES Input: N = 123 O
    7 min read
  • Check if given number is a power of d where d is a power of 2
    Given an integer n, find whether it is a power of d or not, where d is itself a power of 2.Examples: Input : n = 256, d = 16 Output : Yes Input : n = 32, d = 16 Output : No Method 1 Take log of the given number on base d, and if we get an integer then number is power of d. C/C++ Code // CPP program
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences