Data Structures Practical 19
Question 1
Write a C program to check whether a given string is a palindrome or not using recursion.
strpalindrom.c
C
#include <stdio.h>
#include <conio.h>
#include <string.h>
int isPalindromeRecursive(char str[], int start, int end);
void main()
{
char str[50];
int len;
clrscr();
printf("Enter a string: ");
scanf("%s",str);
len = strlen(str);
if (isPalindromeRecursive(str, 0, len - 1))
{
printf("\nThe string \"%s\" is a Palindrome.\n", str);
}
else
{
printf("\nThe string \"%s\" is NOT a Palindrome.\n", str);
}
getch();
}
int isPalindromeRecursive(char str[], int start, int end)
{
if (start >= end)
{
return 1;
}
if (str[start] != str[end])
{
return 0;
}
return isPalindromeRecursive(str, start + 1, end - 1);
}
Output
Question 2
Write a program in C to copy one string to another using recursion.
strcopy.c
C
#include <stdio.h>
#include <conio.h>
void copyStringRecursive(char source[], char destination[], int index);
void main()
{
char sourceStr[100];
char destStr[100];
clrscr();
printf("Enter the source string: ");
scanf("%s",sourceStr);
copyStringRecursive(sourceStr, destStr, 0);
printf("\nSource String: %s", sourceStr);
printf("\nDestination String: %s\n", destStr);
getch();
}
void copyStringRecursive(char source[], char destination[], int index)
{
destination[index] = source[index];
if (source[index] == '\0')
{
return;
}
copyStringRecursive(source, destination, index + 1);
}
Output
