Reverse a string using recursion

Program:

#include <stdio.h>
int reverse(char arr[],int index,int size)
{
    char temp=arr[index];
    arr[index]=arr[size-index];
    arr[size-index]=temp;
    if(index==size/2)
    {
        return arr;
    }
    else
    {
        reverse(arr,index+1,size);
    }
}
int main()
{
    char str1[100];
    printf("Enter the string to reverse :\n");
    scanf("%s",str1);
    int size=strlen(str1);
    reverse(str1,0,size-1);
    printf("String after reversing : %s",str1);
    return 0;
    
}


output:

Enter the string to reverse :                                                                                                                
programming                                                                                                                                  
String after reversing : gnimmargorp    

Comments

Popular posts from this blog

C program to print the following Pattern

c++ program to print string along the diagonals of the matrix.

Kollywood Game