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/
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/
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/
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;
implement the rules in that function, and pass it the input string. Currently you appear to be validating and getting input in the same function, which is just poor design.
Only include teh headers you need, and only use standard headers. Since you are using C++, use teh C++ library where it provides better functionality - like using std::string rather than C style strings.
The algorithm might be something like:
valid = false
if length == 1 and first char is alpha
----valid == true
else if length not zero and length less than 8
----if the first character is _ or alpha
--------if second character is alphanumeic
------------valid = true
------------for all remaining characters while valid == true
----------------if character is not alphanumeric
--------------------valid = false
----------------endif
------------endfor
--------endif
----endif
endif
return valid
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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/
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/
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/
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();
}
> all i need is to combine them as one code that solves the problem above.
Go on then!
I'm going to ignore your code, since it is probably not the best approach. I suggest that you create a single function:
bool validateIdentifier( std::string identifier )
{
...
}
implement the rules in that function, and pass it the input string. Currently you appear to be validating and getting input in the same function, which is just poor design.
Only include teh headers you need, and only use standard headers. Since you are using C++, use teh C++ library where it provides better functionality - like using std::string rather than C style strings.
The algorithm might be something like:
valid = false
if length == 1 and first char is alpha
----valid == true
else if length not zero and length less than 8
----if the first character is _ or alpha
--------if second character is alphanumeic
------------valid = true
------------for all remaining characters while valid == true
----------------if character is not alphanumeric
--------------------valid = false
----------------endif
------------endfor
--------endif
----endif
endif
return valid