C program to find out whether a given string is an identifier or not. A string is called an identifier when he follows these rules:
C program to find out whether a given string is an identifier or not.
A string is called an identifier when he follows these rules:
A string is called an identifier when he follows these rules:
- The first letter must be alphabet(both capital or small i.e. A-Z,a-z) or underscore(_).
- After first letter it contains sequence of alphabet or digits(0-9) or underscore(_) but not contain any special symbol(#,@,$,%,^,& etc.) and space( ).
#include <stdio.h>
#include <string.h>
#include <conio.h>
main (){ //code by techbytebox.com
char string [50];
printf ("Enter value to be identified: ");
scanf ("%s", string);
int length = strlen (string) - 1;
bool alpha_string = false;
if ( string [0] >= 'A' && string [0] <= 'Z' )
alpha_string = true;
if ( string [0] >= 'a' && string [0] <= 'z' )
alpha_string = true;
bool underScore = true;
if ( string [length] == '_' )
underScore = false;
int num = 0;
for ( int i = 1; i <= length; i++ ) {
if ( string [i] == '_' )
num++;
else
num = 0;
if ( num == 2 ) {
underScore = false;
break;
}}
if ( alpha_string && underScore)
printf ("Answer: %s is a valid identifier\n", string);
else
printf ("Answer: %s is not a valid identifier\n", string);
getche();
} //code by Afolabi Achiever (techbytebox)
Recommended: Complete Guide on How to install Xampp
I hope your C Program compiles without error?? if so kindly drop a comment and we will be glad to help you out, else share this page with your programming friends through any of the social media listed below.
I hope your C Program compiles without error?? if so kindly drop a comment and we will be glad to help you out, else share this page with your programming friends through any of the social media listed below.
COMMENTS