Print reverse of a string using recursion.

    by nikoo28

    Question: Write a program in C to reverse a string using recursion

    Input: Hello Everyone
    Output: enoyrevE olleH

    We will use the simple concept of recursion to print the string in reversed order. Note that, we will not store the reversed string in any variable, we are just printing the string in reversed order.

    #include<stdio.h>
    
    //defining a function to print a string in reverse order
    void reversePrint(char *str)
    {
    	// this is the terminating condition of the recursive loop.
    	while(*str != '\0')
    	{
    		// here is the recursive step,
    		// we call the string with its next character.
    		reversePrint(str+1);
    
    		// once all the recursions are complete,
    		// the program returns here and starts printing the
    		// characters one by one
    		printf("%c",str);
    	}
    }
    
    int main(void)
    {
    	char *s=(char *)malloc(sizeof(char)*100);
    
    	//we use this statement so that spaces are scanned
    	scanf("%[^\n]s",s);
    
    	reversePrint(s);
    
    	return 0;
    }
    

    Recursive function (reversePrint) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed location (str) and return one by one.

    Method 2:- Reverse a string without using recursion.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Write a program that changes the case of each character in a string. If its uppercase, convert it to lowercase and vice-versa. Input: ThE quiCK brOwN FOX jUMPs. Output: tHe QUIck BRoWn fox Jumps. In this program we shall apply the basic principle that each character is represented as an integer ASCII code. For more reference look into the post – Find the case of a character input by the user. Now we know that ‘a’ -> ASCII CODE = 97 ‘A’ -> ASCII CODE = 65 Thus the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Write a program that returns the case of the character? Input: e Output: lower case We shall use the concept of ASCII characters to determine the case of the character input by the user. The ASCII table can be found at this link. ASCII TABLE From this we can see the following ASCII codes:- CHARACTER ASCII CODE A 65 Z 90 a 97 z 122 Thus, to check the case we simply compare the ASCII codes. Since we are using C language, in C every character is considered as …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Merge Sort

    by nikoo28
    5 minutes read

    Merge Sort is one of the most popular methods of sorting an array and as offers a constant operating time of . And, as the name suggests it involves merging of several sorted arrays to combine and form a single sorted arrays in the end. The steps involved in merge sort are:- Divide the array in 2 parts left and right. Now recursively divide the left and right parts into more left and right sub parts. Keep doing step 2 , until the sub parts are no greater than a …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    How to merge 2 sorted arrays?

    by nikoo28
    3 minutes read

    Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr1[] = 1, 4, 6, 8, 13, 25    ||     arr2[] = 2, 7, 10, 11, 19, 50 Output: 1, 2, 4, 6, 7, 8, 10, 11, 13, 19, 50 One of the simplest ways to solve this problem would be to copy both the arrays into a new array and then apply some sorting technique on them. But this method will not utilize the fact that both the arrays are already sorted. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Bubble Sort

    by nikoo28
    4 minutes read

    Bubble sort is one of the simplest sorting algorithms and it works on the principle of swapping two elements. Think of the bubble sort as a water bubble that arises from the bottom of the ocean. As it rises up, it grows in size and its the maximum when it reaches the surface. Similarly, in bubble sort, we arrange items from smallest to the largest at the end one by one in each iteration.The algorithm for bubble sort is as follows:- We start from the bottom of the array and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Selection Sort

    by nikoo28
    3 minutes read

    As the name suggests SELECTION SORT involves selecting an element. Now the question arises, as to how should we select this element. What is the criteria? Where should we put it? All these answers are given in the algorithm for SELECTION SORT. In selection sort, what we do is:- Start from the first position in the array. Traverse the remaining array to find the smallest number Swap this smallest number with the number we selected in the first place. Repeat steps 2 and 3 with the next position. Let us …

    1 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More