0

i am also seeing the similar problem.. the code is here..

#include<stdio.h>
#include<string.h>
int palindrome(char *a,int n);
int main()
{
char a[20];
int b,n;
printf("Enter the string to be checked\n");
scanf("%s",a);
printf("The entered string is ");
puts(a);
printf("%c%c%c",a[0],a[1],a[2]);
//empty line
strlen(a);
b=palindrome(a,n);
if(b==0)
printf("%s is palindrome\n",a);
else
printf("%s is not palindrome\n",a);
}
int palindrome(char *a,int n)
{
int i=0;
int j=n-1;
for(;i<j;i++,j--)
{
//no empty line
//printf("%d",i);
printf("%c",a[i]);
printf("%c",a[j]);
//printf("%d",strcmp(a[i],a[j]));
if(a[i]!=a[j])
return(0);
}
return(1);
}

i wrote similar code in other format...

#include<stdio.h>
#include<string.h>
int palindrome(char *a, int n);
int main()
{
char a[20];
//char b[20];
int c,n;
printf("Enter the string to check");
scanf("%s",a);
//scanf("%s",b);
//printf("The entered string is ");
puts(a);
//puts(b);
n=strlen(a);
c=palindrome(a,n);
if(c==0)
printf("The string is palindrome");
else
printf("The string is not palindrome");
}
int palindrome(char *a, int n)
{
int i=0,j=n-1;
for(;i<j;i++,j--)
//{
//printf("%c  ",a[i]);
//printf("%c",a[j]);
if(a[i]!=a[j])
return(1);
//}
return(0);
}

The second one is working.. the first one returns error of segmentation fault core dump.

I am using cygwin gcc recent version for compilation. The error file is generated for first code upon executing with extn of .stackdump.

The error file is

Exception: STATUS_ACCESS_VIOLATION at eip=00401144
eax=49662080 ebx=61227A1E ecx=6115B320 edx=00000000 esi=61227A2D edi=00000000
ebp=0022CCF8 esp=0022CCE0 program=C:\cygwin\home\LP-0126\palindrome.exe, pid 3536, thread main
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame Function Args
0022CCF8 00401144 (0022CD40, 49435341, 00000065, 0000006E)
0022CD68 004010D6 (00000001, 61227AA4, 006700F8, 610049BE)
0022CD98 61006E73 (00000000, 0022CDD4, 61006720, 7FFD5000)
End of stack trace

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by wisaacs
0

Nice elegant function for 'palindrome'.
Your string buffer is completely unprotected.
Writing safe string code in C isn't too hard, but you have to change most of your function calls. First, if you allocate fixed arrays, 'sizeof' is your friend.

// use a reasonable buffer-size.  RAM is cheap.  Any single variable can go to 4096 without thinking.
#define BUFFERSIZE 250
	assert( BUFFERSIZE > 1 );
	char a[BUFFERSIZE];
	size_t last= sizeof(a)-1; // with a fixed array this always gets last byte

The scanf function is dangerous. One way to protect your buffer is to provide a length limiter in the "%s" string format, as in "%20s". Here is some code to do that:
Also, always terminate a string buffer after input, because some functions don't do it for you.

char fmt[100]; // enough to hold any number
	sprintf_s(fmt,sizeof(fmt),"%%%ds",last); // an array of size 10 will yield "%9s".
// always terminate a string returned from a function
	fmt[ sizeof(fmt)-1 ] =0; 
	printf("generated format string: \"%s\"\n",fmt);

Now you have a format string that guarantees 'scanf' won't clobber your buffer.

scanf(fmt,a); // get user input
         // now, to get the length of user-generated input.
         // strlen, right?  not so fast..
         // size_t n= strlen(a); // no! very dangerous. 'a' could be unterminated
	a[last]=0; // force-terminate the string.  It's cheap and easy.
         size_t n= strlen(a); // okay now, because string will never overrun.
	printf("input: \"%s\"\n",a);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.