Not quite:
ABC is not a palindrome but ABC and ABC would compare to itself just fine.
You are on the right track, comparing first to last. Any idea of how to use recursion for that?
#include <stdio.h>
#include <string.h>
void substringMinusOne(char* string){
/*change Alila to lila here*/
}
int palindrome(char* s1, char* s2){
if (strlen(s1) == 1 && strlen(s2) == 1 && strcmp(s1,s2) == 0)
return 1;
else if (strlen(s1) == 1 && strlen(s2) == 1 && strcmp(s1,s2) != 0)
return 0;
else
return 1 * palindrome(substringMinusOne(s1), substringMinusOne(s2));
}
int main(void) {
char* s1 = "Alila";
char* s2 = "alilA";
//char* result = substring(s1);
int result = palindrome(s1, s2);
if (result == 1)
printf("%s is a palindrome of %s\n", s1, s2);
else
printf("%s is not a palindrome of %s2\n", s1, s2);
return 0;
}
// I would skip the substringMinusOne function and just do
else
return palindrome(s1+1, s2-1);
#include <stdio.h>
#include <string.h>
int palindrome(char* s1, char* s2){
if (strlen(s1) == 1 && strlen(s2) == 1 && strcmp(s1,s2) == 0){
return 1;
}
else if (strlen(s1) == 1 && strlen(s2) == 1 && strcmp(s1,s2) != 0)
return 0;
else{
char result[strlen(s1)-1];
char result1[strlen(s2)-1];
strcpy(result, s1+1);
strcpy(result1, s2+1);
return 1 * palindrome(result, result1);
}
}
int main(void) {
char* s1 = "Alila";
char* s2 = "pentil";
if (palindrome(s1, s2))
printf("%s is a palindrome of %s\n", s1, s2);
else
printf("%s is not a palindrome of %s2\n", s1, s2);
return 0;
}
else
return palindrome(s1+1, s2-1);
char* s1 = "ABC"
char* s2 = "ABC";
while(*s2 != '\0' && *(s2+1) != '\0')
s2++;
char* s1 = "ABC"
s2 = s1;
while(*s2 != '\0' && *(s2+1) != '\0')
s2++;
int isPalindrome(char* s1, char*s2){
if (s1 <= s2)
return true;
else if (s1[0] != s2[0])
return false;
else if (s2-s1 > 1)
return palindrome(s1+1, s2-1);
}
}
Title | # Comments | Views | Activity |
---|---|---|---|
how to understand recursion | 12 | 206 | |
Is this a possibly new algorithm using C++ to provide an inorder iterator for Binary Search Trees? | 8 | 77 | |
Problem with SqlConnection | 5 | 123 | |
Header Errors LNK2019, LNK1120 - Unresolved Externals | 4 | 61 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
10 Experts available now in Live!