Home > Back-end >  Ask the fgets and gets problem.
Ask the fgets and gets problem.

Time:11-14

#include //1 code is palindrome judgment characters of
#include //2 code and code only gets 1 and the difference of the fgets,
Int isPalindrome (char str1 [], int n)//when the input aba, 1 code output is YES, 2 of the code is NO, could you tell me why will appear this kind of problem?
{//I am a freshman, learn c language, hope to have a detailed explanation,
Char str2 [999].//gets go to school books, but has warned that the fgets is when I was a student in baidu, but what is the problem here,
int i;
int len;
Len=strlen (str1);
for(i=0; i{
Str2 [I]=str1 [-i len - 1];
}
for(i=0; i<=len; I++)
{
If (str1 [I]!=str2 [I])
break;
}
If (ireturn 0;
The else
return 1;
}
Int main (int arg c, char * argv [])
{
Char str1 [999].
Int x;
Gets (str1);
X=isPalindrome (str1, 1000);
If (x==1)
Printf (" YES ");
The else
Printf (" NO ");
return 0;
}






#include
#include
Int isPalindrome (char str1 [], int n)
{
Char str2 [999].
int i;
int len;
Len=strlen (str1);
for(i=0; i{
Str2 [I]=str1 [-i len - 1];
}
for(i=0; i<=len; I++)
{
If (str1 [I]!=str2 [I])
break;
}
If (ireturn 0;
The else
return 1;
}
Int main (int arg c, char * argv [])
{
Char str1 [999].
Int x;
The fgets (str1, 1000, stdin);
X=isPalindrome (str1, 1000);
If (x==1)
Printf (" YES ");
The else
Printf (" NO ");
return 0;
}

CodePudding user response:

When using the s [strlen (s) - 1]='\ 0' method, after school, a data detection of the CG system error, output have a data request is yes, but the output is no, but I don't know what data is wrong,
  • Related