//Palindrome for integers
#include <stdio.h>
int main()
{
int num, rev = 0, rem, orig;
printf("Input integer number: ");
scanf("%d", &num);
orig = num;
while (num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
if (orig == rev)
printf("%d is a palindrome_integer.", orig);
else
printf("%d is not a palindrome_integer.", orig);
return 0;
}
Output: Input integer string 111 111 is a palindrome_integer
//Palindrome for strings
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char p[100];
int i,b, m, e, l = 0;
printf("Input character string");
gets(p);
while (p[l] != '\0')
l++;
e = l - 1;
m = l/2;
for (b = 0; b < m; b++)
{
if (p[b] != p[e])
{
printf("Input character string is not a palindrome.\n");
break;
}
e--;
}
if (b == m)
printf("Input character string is Palindrome.\n");
return 0;
}
Output: Input character string Civic Input character string is not a palindrome.
//checking palindrome using string functions
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
char a[10], b[10],rev[10];
int i;
printf("Enter a string to check if it's a palindrome\n");
scanf("%s",&*a);
strcpy(b, a);
long int length = strlen(b);
for(i=0; i<length; ++i)
{
rev[length-i-1] = b[i];
}
if (strcmp(a, rev) == 0)
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
return 0;
}
//checking palindrome using stack and queue
#include <stdio.h>
#include <stdlib.h>
struct plaindrome_Node{
char data;
struct plaindrome_Node *p_next;
};
typedef struct plaindrome_Node *node_ptr;
struct plaindrome_Node* get_Node()
{
struct plaindrome_Node *new_Node = (struct plaindrome_Node*)malloc(sizeof(struct plaindrome_Node));
return new_Node;
}
void freeNode(node_ptr *p_q)
{
free(p_q);
}
void push(node_ptr *p_q, char p_x)
{
node_ptr f = *p_q;
*p_q = get_Node();
(*p_q)->data = p_x;
(*p_q)->p_next = f;
}
char pop_q(node_ptr *p_q)
{
node_ptr f = *p_q;
char p_val = 0;
if(f == NULL)
{
//Blah blah
return p_val;
}
if(f -> p_next == NULL)
{
p_val = f->data;
*p_q = NULL;
return p_val;
}
else
{
//Traverse to last node
while((f->p_next)->p_next != NULL)
{
f = f->p_next;
}
p_val = (f->p_next) -> data;
f -> p_next = NULL;
free(f -> p_next);
return p_val;
}
}
char pop_stack(node_ptr *p_s)
{
node_ptr f = *p_s;
char p_val = 0;
if(f == NULL)
{
//Blah blah
return p_val;
}
else
{
p_val = f -> data;
*p_s = f -> p_next;
return p_val;
}
}
int main()
{
node_ptr p_Queue;
node_ptr p_Stack;
char strr[32];
char b,f;
int i;
p_Queue = 0;
p_Stack = 0;
printf("%s\n","Input string");
scanf("%s",strr);
//insert into queue and stack
i = 0;
while((b = strr[i]) != '\0')
{
push(&p_Queue, b);
push(&p_Stack, b);
i++;
}
//pop and check
i = 1; //Assume palindrome
b = 1;
f = 1;
while(b != 0 && f != 0 && i == 1)
{
b = pop_stack(&p_Stack);
f = pop_q(&p_Queue);
if(b != f)
i = 0;
}
if(i == 1)
printf("Input string is a palindrome\n");
else
printf("Input string is not a palindrome\n");
return 0;
}
Output: Input character string Civic Input string is a palindrome





Leave a Reply