Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

Reverse the given String

Get a String and reverse the characters of the string

Sample Input 1:

Hello

Sample Output 1:

olleH

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