In: Computer Science
Create program which verifies if input string is a valid variable declaration or not. Use C programming language.
- This program can only use the following variable types: char, float, and int
- Remove any newline \n from input string
- The input prompt should say ">>> "
- If input declaration is valid, it should print "Valid dec\n"
- If input declaration is invalid, it should print "Invalid dec\n"
- Make sure the identifier entered matches the rules of C identifiers
- No need to worry about value assignments (Ex. int i = 0;)
Example Runs:
• Invalid identifier.
>>> char 3var; Invalid dec
• Invalid type.
>>> double my_var; Invalid dec
• Valid declaration.
>>> int my_int; Valid dec
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[25];
int count=0,flag;
printf("enter any string: ");
scanf("%s",string);
printf(">>>",string);
if((string[0]>='a'&&string[0]<='z')||(string[0]>='A'&&string[0]<='Z')||(string[0]=='_'))
{
for(int i=1;i<=strlen(string);i++)
{
if((string[i]>='a'&&
string[i]<='z')||(string[i]>='A' &&
string[i]<='Z')||(string[i]>='0'&&
string[i]<='9')
||(string[i]=='-')){
count++;
}}
if(count==strlen(string))
{
flag=0;
}
}
else
{
flag=1;
}
if(flag==1)
printf("Invalid dec\n");
else
printf("Valid dec\n");
}
Note:***I hope your happy with my answer***If you have any doubts please comment me****Thank you....