Thursday, December 18, 2014

Palindrome or not using Recursion in c pgm



Find the First Capital Letter in a String using Recursion

#include 
#include 
#include 
 
char caps_check(char *);
 
int main()
{
    char string[20], letter;
 
    printf("Enter a string to find its first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        static int i = 0;
        if (i < strlen(string))
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            else
            {
                i = i + 1;
                return caps_check(string);
            }
        }
        else return 0;
    }
Output
Enter a string to find its first capital letter: iloveC
The first capital letter in iloveC is C.

Find the First Capital Letter in a String without using Recursion

#include 
#include 
#include 
#include 
 
char caps_check(char *);
 
int main()
{
    char string[20], letter;
 
    clrscr();
    printf("Enter a string to find its first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        int i = 0;
        while (string[i] != '\0')
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            i++;
        }
        return 0;
    getch();
    }
 
Output
 
Enter a string to find its first capital letter: prOgraMmInG
The first capital letter in prOgraMmInG is O.

 

Find the Length of the String using Recursion

#include 
#include 
#include 
 
struct node
{
    int a;
    struct node *next;
};
 
void generate(struct node **);
int length(struct node*);
void delete(struct node **);
 
int main()
{
    struct node *head = NULL;
    int count;
 
    generate(&head);
    count = length(head);
    printf("The number of nodes are: %d\n", count);
    delete(&head);
    return 0;
}
 
void generate(struct node **head)
{
    int num = 10, i;
    struct node *temp;
 
    for (i = 0; i < num; i++)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = i;
        if (*head == NULL)
        {
            *head = temp;
            (*head)->next = NULL;
        }
        else
        {
            temp->next = *head;
            *head = temp;
        }
    }
}
 
int length(struct node *head)
{
    if (head->next == NULL)
    {
        return 1;
    }
    return (1 + length(head->next));
}
 
void delete(struct node **head)
{
    struct node *temp;
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}
Output
 
        The number of nodes are: 10

 

Check whether a given String is Palindrome or not using Recursion

#include 
#inclue 
#include 
 
void check(char [], int);
 
int main()
{
    char word[15];
    clrscr();
    printf("Enter a word to check if it is a palindrome\n");
    scanf("%s", word);
    check(word, 0);
 
    return 0;
}
 
void check(char word[], int index)
{
    int len = strlen(word) - (index + 1);
    if (word[index] == word[len])
    {
        if (index + 1 == len || index == len)
        {
            printf("The entered word is a palindrome\n");
            return;
        }
        check(word, index + 1);
    }
    else
    {
        printf("The entered word is not a palindrome\n");
              }
            }
            getch();
            }
 
 
Output
 
Enter a word to check if it is a palindrome
malayalam
The entered word is a palindrome

No comments:

Post a Comment