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

For any given matrix find the path from the start to the end which gives the maximum sum. Traverse only right or down. Example: starting index is 15 (left top) and ending index is 10 (bottom right) 15 25 30 45 25 60 70 75 10 O/P:15->45->70->75->10 sum is 215

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