Skip to content

Instantly share code, notes, and snippets.

@moomdate
Created October 21, 2019 09:11
Show Gist options
  • Save moomdate/5d0cdf01ea5519a60861797c59e0f551 to your computer and use it in GitHub Desktop.
Save moomdate/5d0cdf01ea5519a60861797c59e0f551 to your computer and use it in GitHub Desktop.
#include<string.h>
#include<stdio.h>
#define true 1
#define flase 0
typedef int bool;
bool isPalindrome(char*);
void findPalindrome(char * dataInput);
int main(){
char * input = "1123456";
if(isPalindrome(input)){
printf("all is palindrome\r\n");
}else{
printf("is not palindrome\r\n");
}
findPalindrome(input);
return 0;
}
// find longest palindrome using scope
void findPalindrome(char * dataInput){
int i,beginIndex = 0,endIndex=5;
int InputSize = strlen(dataInput)+1;
//char * temp_Scope = dataInput;
char *temp_Scope = malloc(sizeof(char)+ InputSize);
//strcpy(copy, tail);
//printf("%s",temp_Scope);
//printf("data is %s",temp_Scope);
for( i = InputSize; i >= 2; i--){
strncpy(temp_Scope, dataInput + beginIndex, i - beginIndex); //copy
//printf("%s\r\n",temp_Scope);
//free(temp_Scope);
if(isPalindrome(temp_Scope)){
printf("%s",temp_Scope);
}
memset(temp_Scope,0,InputSize); //clear
}
}
/*
palindrome checking
return boolean;
*/
bool isPalindrome(char * data){
int a = 0;
int count = 0;
bool st = flase;
char * temp = data;
int dataLength = strlen(data);
for(a = dataLength - 1; a >= 0 ; a--){
//printf(" data %c \r\n",temp[a]);
//printf("temp %c data %c \r\n",temp[a],data[dataLength-a-1]);
if(temp[a] == data[dataLength-a-1])
count++;
else
break;
}
//printf("count %d size %d",count,dataLength);
if(count == dataLength)
st = true;
return st;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment