How to check whether a string is palindrome or not using recursion in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

void check_palindrome_recursion(char s[], int index) {
    int fromend = strlen(s) - (index + 1);
    
    if (s[index] == s[fromend]) {
        if (index + 1 == fromend || index == fromend) {
            printf("Palindrome\n");
            return;
        }
        check_palindrome_recursion(s, index + 1);
    }
    else {
        printf("Not palindrome\n");
    }
}

int main() {
    char s[10] = "abcdcba";

    check_palindrome_recursion(s, 0);
    
    return 0;
}




/*
run:

Palindrome

*/

 



answered Jan 16, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int palindrome_recursion(char s[], int index) {
    int fromend = strlen(s) - (index + 1);
    
    if (s[index] == s[fromend]) {
        if (index + 1 == fromend || index == fromend) {
            return 1;
        }
        palindrome_recursion(s, index + 1);
    }
    else {
        return 0;
    }
}

int main() {
    char s[10] = "abcdcba";

    if (palindrome_recursion(s, 0)) {
        printf("Palindrome\n");
    }
    else {
        printf("Not palindrome\n");
    }
    
    
    return 0;
}




/*
run:

Palindrome

*/

 



answered Jan 16, 2021 by avibootz

Related questions

1 answer 215 views
2 answers 177 views
1 answer 134 views
1 answer 154 views
1 answer 175 views
1 answer 188 views
...