What is the c++ code for recognizing a valid c++ identifier?

What is the c++ code for recognizing a valid c++ identifier? there are some conditions to be followed:
1. it should start with a letter or an underscore
2. no two succeeding underscore.
3.the length of the identifier is from 1 to 8 characters only.
4. a single underscore inputted as an identifier is invalid.

here are the codes that i coded that are very much familiar with the problem. all i need is to combine them as one code that solves the problem above.:

/*THIS IS A SIMPLE CODE TO RECOGNIZE AN IDENTIFIER*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
using namespace std;

void error();
/*THIS IS A SIMPLE CODE TO RECOGNIZE AN IDENTIFIER*/
char in;
void
error()
{

in=getchar();

 if(isalpha(in))
  in=getchar();
 else
 {error();}
 
 
 while(isalpha(in)||isdigit(in))
  in=getchar();

}
int
main ()
{
  
  
  error();
  
  {
  getch();
  }
}

;;;;;;;;;;;;;;
/*THIS IS A SIMPLE CODE that RECOGNIZE a REAL NUMBER*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <ctype.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
using namespace std;
void error();
/*THIS IS A SIMPLE CODE that RECOGNIZE a REAL NUMBER*/
char in;
void
error()
{
   in=getchar();
  if(in=='+'||in=='-')
   in=getchar(); 
  while (isdigit(in))
   in=getchar();
  if(in=='.')
   in=getchar();
  else
   error();
  if(isdigit(in))
   in=getchar();
  else
   error(); 
  while(isdigit(in))
   in=getchar();
   cout<<"ok\n";
}
int
main ()
{
  
  
  error();
  
  {
  getch();
  }
}

;;;;;;;;;;;;;;;
/*THIS IS A SIMPLE CODE That implements a RECOGNIZER for AN IDENTIFIER*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <ctype.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
using namespace std;


/*THIS IS A SIMPLE CODE That implements a RECOGNIZER for AN IDENTIFIER*/
void error();
int main()
{
int state;
char in;

  state=1;
  in=getchar();
 while(isalpha(in)||isdigit(in))
 {
  switch(state)
   {
   case 1:if(isalpha(in))
             state=2;
          else
             error();
          break;
   case 2:state=2;
          break; 
   }
   in=getchar();
 }
 return(state==2);
  {
  getch();
  }
}
void 
error()
 {
  getch();
 }

1. it should start with a letter or an underscore
2. no two succeeding underscore.
3.the length of the identifier is from 1 to 8 characters only.
4. a single underscore inputted as an identifier is invalid.

Wrong.
1. It must start with a Latin alphabet character (A-Z and a-z) or an underscore (_).
2. After the first character, it may contain only Latin alphabet characters, Arabic numerals (0-9), or underscores.
3. There's no limit to the length of the identifier.
4. They're case-sensitive
Therefore: a is valid; A is valid and also a different identifier; 1 is invalid; _ is valid; __ is valid; _1 is valid; __1 is valid; 1_ is invalid.

I'm sorry to say this, but your functions suck.
Last edited on
Topic archived. No new replies allowed.