VOOZH about

URL: https://www.geeksforgeeks.org/theory-of-computation/program-to-construct-a-dfa-to-check-if-a-given-integer-is-unsigned-or-not/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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:

👁 Image

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:


Output: 
Unsigned integer

 

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

Comment
Article Tags:

Explore