Reverse the given String

Get a String and reverse the characters of the string

Sample Input 1:

Hello

Sample Output 1:

olleH

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include<stdio.h>
#include<string.h>
int main()
{
	char str[50],temp;
	int i = 0;
	int j;
	fgets(str,50,stdin);
	j = strlen(str) - 1;
	while(i<j)
	{
		temp=str[i];
		str[i]=str[j];
		str[j]=temp;
		i++;
		j--;
	}
	printf("%s",str);
	return 0;
}
			
				
			

Program Explanation

initialize i to first location of string and j to last location of string using i=0 j=n-1

swap the characters in location i and j, then increment i by 1 and decrement j by 1.

t=str[i] str[i]=str[j] str[j]=temp repeat the above step till i is less than j

Comments