ok, basically i want to be able to input a string and find out if its a valid c identifier.......well, i got that part to work, the part that just wont come together is when i input ctrl+Z to end the program, it doesnt work???....can canybody help?? THANKS
//using msvs6.0
Code:
#include <stdio.h>
int StrSpn(char *str, char *cset)
{
char *strPTR;
char *csetPTR;
strPTR = str;
csetPTR = cset;
while((*strPTR) != '\0') /* while strPTR != null and != to a space*/
{
while((*csetPTR) != '\0' && (*strPTR) != (*csetPTR)) /*whle csetPTR != to null & strPTR != csetPTR*/
{
csetPTR++; /*increment csetPTR*/
}
if ((*strPTR)==(*csetPTR)) /*if strPTR = csetPTR*/
{
strPTR++; /*increment strPTR*/
csetPTR = cset; /*reset the value of csetPTR to the value of cset*/
}
else /*else, the end of the string is not NULL*/
{ /*break the loop*/
if((*strPTR) != '\0');
{
return 3;
}
break;
}
}
return 0;
}
int IsDigit(int ch)
{
/*if the value at the first address of the string inputed is a number
** the program will print that it is not a valid c identifier and return 0 */
if( (ch >='0') && (ch <= '9') )
return 0;
if (ch == ' ')
return 0;
return 1;
}
int IsIdentifier(char *str)
{
if(str != '\0')
{
if(StrSpn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789") == '\0')
{
return 1; /*when strspn gets to the end of the enterd string*/
/*and all characters are valid, 1 is returned*/
}
else
{
return 0; /* if a digit which is not valid is found 0 is returned*/
}
}
return 0;
}
int main()
{
char identifier[255];
printf("The purpose of this program is to determine \n");
printf("if the string entered is a valid c identifier\n");
printf("Press Ctrl Z to exit the program\n");
printf("Please Press Enter to Continue.\n");
while (gets(identifier) != NULL)
{
printf("Enter an identifier: ");
scanf("%[^\n]", identifier);
/*Runs the IsDigit function, if the value of*/
/*IsDigit returns a zero, end the program*/
if(IsDigit(identifier[0]) == 0)
{
printf("\nThe string is NOT a valid c identifier\n\n");
}
else if(IsIdentifier(identifier) == 1)
{
printf("The string is a VALID C IDENTIFIER\n\n");
}
else if(IsIdentifier(identifier) == 0)
{
printf("The string is NOT a valid c identifier\n\n");
}
return 0;
}