Created
August 22, 2018 15:51
-
-
Save meldsza/8be4fa3d1e52ea233ccaaeb2cbf2865a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define MAX_SIZE 100 | |
struct | |
{ | |
int top; | |
char items[MAX_SIZE]; | |
} typedef Stack; | |
char pop(Stack* s) | |
{ | |
if(s->top == -1) | |
return 0; | |
return s->items[s->top--] ; | |
} | |
void push(Stack* s, char item) | |
{ | |
s->items[++s->top] = item; | |
} | |
/** | |
* @function cmp_alphanum | |
* @returns (int) returns 1 if they are equal. 0 if they are not equal | |
* @argument a (char[]) first string | |
* @argument b (char[]) second string | |
* Compare two string ignoring spaces and special charectors | |
*/ | |
int cmp_alphanum(char a[MAX_SIZE], char b[MAX_SIZE]) | |
{ | |
int i=0, j=0, n = strlen(a); | |
while(i<n &&j<n) | |
{ | |
while(i<n && a[i]==' ') | |
i++; //increment i until no spaces are encountered | |
while(j<n && b[j]==' ') | |
j++; //increment j until no spaces are encountered | |
if(a[i]!=b[j]) | |
return 0;//they are not equal. return 0 | |
i++;//increment i | |
j++;//increment j | |
} | |
return 1;//they are equal. return 1 | |
} | |
int main() | |
{ | |
char orginal_string[MAX_SIZE] = {0}, final_string[MAX_SIZE] = {0}; | |
char *temp; | |
Stack s; | |
s.top = -1; | |
puts("Enter a string"); | |
gets((char*)orginal_string); | |
if(strlen(orginal_string) == 0){ | |
puts("Enter valid string"); | |
return 0; | |
} | |
temp = orginal_string; | |
while(*temp != 0) | |
{ | |
push(&s,*temp); | |
temp++; | |
} | |
temp = final_string; | |
while((*temp = pop(&s))){ | |
temp++; | |
} | |
*(++temp) = 0; | |
printf("Reverse of given string is\n%s\n",(char*)final_string); | |
if(cmp_alphanum(orginal_string, final_string)) | |
puts("String is a palindrome"); | |
else | |
puts("String is not a palindrome"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment