Home > Back-end >  Novice C language a beginners don't know where I went wrong, ask for help
Novice C language a beginners don't know where I went wrong, ask for help

Time:09-23

Given a string does not contain whitespace, please determine whether C legal identifiers (note: subject to ensure the string must not C language reserved word),

C language identifier requirements:

1. The reserved word;

2. Include only letters, Numbers and underscore (" _ "),

3. Don't begin with Numbers,

Input:

A line, contains a string, the string does not contain any whitespace, and the length is not more than 20,

Output:

A line, if it is the legal identifier of the C language, the output yes, otherwise no output,

The sample input:

RKPEGX9R; TWyYcp

The sample output:

No

I write so
# include & lt; String. H>
# include & lt; Stdio. H>

Int h (char a)
{
If ((a>='a' & amp; & A<='z') | | (a>='A' & amp; & A<='Z') | | (a>='0' & amp; & A<='9'))
{return 1; }
return 0;
}

Int f (char a)
{
If (a>='a' & amp; & A<='z' | | a>='A' & amp; & A<='Z')
{return 1; }
return 0;
}

Int main ()
{
Int I, CNT;
Char sym [20].
Gets (sym);
If ((sym [0]!='_') & amp; & (! F (sym [0])))
{
Puts (" no ");
return 0;
}
For (I=1, CNT=1; Cnt<20; Cnt++)
{
If (h (sym/CNT) | | (sym (CNT)='_'))
{I=1; }
The else
{I=0;
break; }
}
If (I==1)
{puts (" yes "); }
The else
{puts (" no "); }


return 0;
}

But the output is yes
Want to half a day do not know where to write wrong, please guide

CodePudding user response:

Can anyone tell me?

CodePudding user response:

this should be no.==

CodePudding user response:

refer to the second floor @ big fool @ reply:
no. It's supposed to be==

But the input the correct also show that the no

CodePudding user response:

You have a problem for loop exit condition
this is less than 20 but you may not enter 20 characters, so your exit condition is the element of the string is not equal to '\ 0'

CodePudding user response:

Your exit condition in the for loop has a problem, you are no more than 20 have been cycle, if you only have three characters, the rest is not worth so you just need to access these three characters, the end of the string is' \ 0 'so long as is not equal to' \ 0 ', will visit
  • Related