Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:28:27+00:00 2026-05-26T20:28:27+00:00

I have been writing a code that either accepts or rejects a string of

  • 0

I have been writing a code that either accepts or rejects a string of input symbols as part of a specified language. And I have written code for the first language but it doesn’t accept the right thing and i was wondering if any one could give me a hint as to where i went wrong. thanks

Question: Why wont the language be accepted or rejected correctly?

Thanks

My code:

#include <stdio.h>

static final char initial_state = '0';
static final char 0 = '0';
static final char 1 = '1';
static final char 2 = '2';
static final char 3 = '3';

int main(int argc, char* argv[]){
  int x;
  char current_state, next_state, initial_state;
  current_state = initial_state;

  printf("Enter a string of characters: ");
  while(scanf("%d", &x)!=EOF){
     switch(current_state){
      case 0: /*initial state*/
       switch(current_state){
       case'0':next_state=1; break;
       case'1':next_state=0; break;
       }
       break;
     case 1: /*Last input was 0*/
      switch(current_state){
      case'0':next_state=1; break;
      case'1':next_state=2; break;
      }
      break;
     case 2: /*Last input was 1*/
      switch(current_state){
      case'0':next_state=3; break;
      case'1':next_state=0; break;
      }
      break;
     case 3: /*Last input was 0*/
      switch(current_state){
      case'0':next_state=3; break;
      case'1':next_state=3; break;
      }
      break;
     }
     current_state=next_state;
    }
  if((current_state==next_state)){
    printf("Language 1 accepts");
  }else if((current_state!=next_state)){
    printf("Language 1 rejects");
  }
  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:28:28+00:00Added an answer on May 26, 2026 at 8:28 pm

    You have defined initial_state twice, and the local one is winning the “scope war.” So in your code, every time you think you’re referring to this initial_state:

    static final char initial_state = '0';
    

    you’re actually referring to this initial state:

    char current_state, next_state, initial_state;  // this last guy here
    

    Additionally, you’re doing some numerics and some characters. You want all characters since you’re taking input from the keyboard. Any place that you define a state as a 1 or a 0, put single quotes around it so it’s a '1' or a '0'.

    Then, take out the code that redefines 1 = '1' for all the states; I do believe you’re asking the program to redefine the number 0x1 to mean the number 0x41 — that’s crazy.

    Here’s the final result (badly formatted):

    #include <stdio.h>
    
    static const char initial_state = '0';
    static const char accepting_state = '3';
    int main(int argc, char* argv[]){
      int x;
      char current_state;
    
      current_state = initial_state;
      printf("Enter a series of characters (0 or 1)\n");
      printf("Press <ctrl-d> to finish.\n");
      printf("> ");
      while(scanf("%d", &x)!=EOF){
         x += '0';
             switch(current_state){
          case '0': /*initial state*/
           switch(x){
           case'0':current_state='1'; break;
           case'1':current_state='0'; break;
           default: goto fail;
           }
           break;
         case '1': /*Last input was 0*/
          switch(x){
          case'0':current_state='1'; break;
          case'1':current_state='2'; break;
          default: goto fail;
          }
          break;
         case '2': /*Last input was 1*/
          switch(x){
          case'0':current_state='3'; break;
          case'1':current_state='0'; break;
          default: goto fail;
          }
          break;
         case '3': /*Last input was 0*/
          switch(x){
          case'0':current_state='3'; break;
          case'1':current_state='3'; break;
          default: goto fail;
          }
          break;
         default: goto fail;
         }
         printf("> ");
      }
    
      if (current_state == accepting_state) {
        printf("Language accepts!\n");
      } else {
        printf("Language rejects.\n");
      }
      return 0;
    
    fail:
      printf("Invalid input\n");
      return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been assigned a project with a lot of poorly written code that
I have been writing Python code for only a couple of weeks, so I'm
I have been writing unit tests using NUnit and Moq with my Silverlight code
I have been messing around with writing some stored procedures in .NET code with
I have been writing DLL on C++, that will be use in C#. DLL
I have been writing some C# code for a training exercise, in which I
Lately, I've been writing a lot of code that looks like this: List<MyObject> myList
I have been writing C++ Console/CMD-line applications for about a year now and would
I have been writing web applications for quite sometime in PHP with MySQL. I
I have been writing Common Lisp macros, so Scheme's R5Rs macros are a bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.