Thursday, October 30, 2014

EXCELLENT C PROGRAM



Reverse the String using Recursion

#include 
#include 
#include 
void reverse(char [], int, int);
int main()
{
    char str1[20];
    int size;
    clrscr();
    printf("Enter a string to reverse: ");
    scanf("%s", str1);
    size = strlen(str1);
    reverse(str1, 0, size - 1);
    printf("The string after reversing is: %s\n", str1);
    return 0;
}

void reverse(char str1[], int index, int size)
{
    char temp;
    temp = str1[index];
    str1[index] = str1[size - index];
    str1[size - index] = temp;
    if (index == size / 2)
    {
        return;
    }
    reverse(str1, index + 1, size);
}

Output

Enter a string to reverse: malayalam
The string after reversing is: malayalam
Enter a string to reverse: cprogramming
The string after reversing is: gnimmargorpc

Copy One String to another using Recursion

#include 
#include 
void copy(char [], char [], int);
 
int main()
{
    char str1[20], str2[20];
    clrscr();
    printf("Enter string to copy: ");
    scanf("%s", str1);
    copy(str1, str2, 0);
    printf("Copying success.\n");
    printf("The first string is: %s\n", str1);
    printf("The second string is: %s\n", str2);
    return 0;
}
 
void copy(char str1[], char str2[], int index)
{
    str2[index] = str1[index];
    if (str1[index] == '\0')
        return;
    copy(str1, str2, index + 1);
}

Output

Enter string to copy: sanfoundry
Copying success.
The first string is: sanfoundry
The second string is: sanfoundry

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.

No comments:

Post a Comment