Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<stdlib.h>
int main()
{
 char a[20];
 int n;
 printf("enter the size of the string  ");
 scanf("%d",&n);
 printf("enter the string ");
 scanf("%s",&a[20]);
 for(int i=0;i<(n-1)/2;i++)
     {
        if(a[i]==a[n-1-i])
            {
                 printf("string is palindrome");
            }
        else
         printf("string is not palindrome");

     }
     return 0;
}

when i am running this program i am getting stack exception...is my code wrong?...i mean is the process wrong? or is the error due to some kind of buffer overflow for using scanf ? Anyone pls help..

================
Edit: Matt T Heffron (code moved from comment below):
i fixed the logic,checked overflow problems but its still not executing...can someone explain...
C++
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
 char a[20];
 int n,c;
 c=0;
 printf("enter the size of the string  ");
 scanf("%d",&n);
 printf("enter the string ");
 fgets(a,n,stdin);

 for(int i=0;i<(n-1)/2;i++)
	 {
		if(a[i]==a[n-1-i])
			{
			 c=0;
			}
		else
		{
		  c=1;
		  break;
		}
	 }

	 if(c==0)
	  printf("string is palindrome");
	 else
	  printf("string is not palindrome");

	 return 0;

	 }
Posted
Updated 12-Nov-15 10:30am
v3
Comments
W Balboos, GHB 11-Nov-15 7:22am    
With the fixes to your solution, below, do not forget that when comparing character that case is important: convert your string to all UC or all LC before commencing the test.

Also, if the string is a sentence, you need to remove punctuation (spaces) before your test, at least for many definitions of palindrome that allow the spacing to be neglected.

Um... try changing this:
C++
scanf("%s",&a[20]);
To this:
C++
scanf("%s",a);
Or even:
C++
scanf("%s",&a[0]);
 
Share this answer
 
C++
#include <stdio.h>
#include <string.h>

int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    
    printf("Enter a string:");
    scanf("%s", string1);
    
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
            flag = 1;
            break;
	   }
	}
    
    if (flag) {
        printf("%s is not a palindrome", string1);
    }    
    else {
        printf("%s is a palindrome", string1);
    }
    return 0;
}


Or with the help of string functions like strrev, strcmp:
C++
#include <stdio.h>
#include <string.h>
 
int main()
{
   char a[100], b[100];
 
   printf("Enter the string to check if it is a palindrome\n");
   gets(a);
 
   strcpy(b,a);
   strrev(b);
 
   if (strcmp(a,b) == 0)
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
 
   return 0;
}


-KR
 
Share this answer
 
v2
Comments
Sam Dean 11-Nov-15 13:42pm    
@KrunalRohit, I AM USING THE SAME LOGIC TO UR 1ST APPROACH...SO WHY IS IT NOT WORKING FOR ME?
jeron1 11-Nov-15 15:11pm    
Your Caps Lock is stuck.
Krunal Rohit 12-Nov-15 0:27am    
Nah. OP wrote @KrunalRohit as well. :laugh:

-KR
Krunal Rohit 12-Nov-15 0:27am    
What's the issue ?

-KR
In addition of the mistake pointed by our Original, your code is logically flawed (it can assert the string is palindrome before the whole string is actually checked).
Try
C
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #define MAX_LEN 20
int main()
{
  char a[MAX_LEN+1];

  printf("enter the string ");
  if ( fgets( a, MAX_LEN+1, stdin) )
  {
    size_t len = strlen(a)-1; // get rid of '\n'
    size_t i;
    printf("n = %d\n", len);
    for(i=0;i<len/2;i++)
    {
      if (a[i]!=a[len-1-i] ) break;
    }
    if ( i == len/2)
      printf("\nstring is palindrome");
    else
      printf("\nstring is not palindrome");

    printf("\n");
  }
  return 0;
}
 
Share this answer
 
v2
Comments
Patrice T 11-Nov-15 11:16am    
I fear the program fail if string length is odd.
CPallini 11-Nov-15 12:00pm    
I guess it works. :-)
Patrice T 11-Nov-15 12:18pm    
Ok, the integer division trick :)
I avoid it when I am not in optimising phase.
Sam Dean 11-Nov-15 14:46pm    
thanks @CPallini...my logic is same as yours..checking the 1st element with the last..excepts that it won't check through the whole string..
Mohibur Rashid 11-Nov-15 19:05pm    
Why do you think that?
Now for something completely different :-)
How about a recursive approach?
C++
#include <stdio.h>
#include <string.h>
#define MAXCHARS 20 // excluding trailing zero character
#define STRINGIZE(a) #a
#define MAX_CHARS_STRING_FMT(len) "%" STRINGIZE(len) "s"

const char* read_string()
{
    static char buffer[MAXCHARS+1]; // allow for a trailing zero character
    scanf(MAX_CHARS_STRING_FMT(MAXCHARS), buffer);
    buffer[MAXCHARS] = '\0'; // paranoid...
    return buffer;
}

int is_palindrom(const char* buffer, size_t len)
{
    return (len < 2) || ((buffer[0] == buffer[len-1]) && is_palindrom(buffer+1, len-2));
}

int main()
{
    const char* buffer = read_string();
    size_t len = strlen(buffer);
    printf("'%s' is %sa palindrom\n", buffer, is_palindrom(buffer, len) ? "": "not "); 
    return 0;
}
Have fun! :-)
Cheers
Andi
 
Share this answer
 
v3
Comments
nv3 12-Nov-15 18:03pm    
A beginner might miss your humor in suggesting something like that - and then use it in production code ;-)
Andreas Gieriet 13-Nov-15 16:12pm    
Agreed. But I'm still puzzled that no one applying scanf() uses scanf("%19s", buffer) with a buffer of 20 chars.
Cheers
Andi
A varying solution from the one of KrunaRohit
C++
#include <stdio.h>
#include <string.h>

int main(){
    char string1[20];
    int i, j, length;
    
    printf("Enter a string:");
    scanf("%s", string1);
    
    length = strlen(string1);
    
    int flag = 1;
    for(i=0, j=length-1; i < j ;i++, j--){
        if(string1[i] != string1[j]){
            flag = 0;
            break;
	   }
	}
    
    if (flag) {
        printf("%s is a palindrome", string1);
    }    
    else {
        printf("%s is not a palindrome", string1);
    }
    return 0;
}

Using traditionnal 8 bits chars will fail if the palindrome include some utf-8 chars.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900