Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Strings » C program to reverse a given string
Suryateja Pericherla Categories: Strings. 1 Comment on C program to reverse a given string
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

In this article we will learn to implement a C program to reverse a given string. A C program is provided below to reverse a given string without using predefined function.

 

Program is as follows:

#include<stdio.h>
#include<string.h>
int main()
{
	char str[20];
	printf("Enter a string: ");
	scanf("%s", str);
	int length = strlen(str);
	for(int i=0, j=length-1; i<j; i++, j--)
	{
		int temp = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	printf("Reversed string is: %s", str);
	return 0;
}

 

Input and output for the above program is as follows:

Enter a string: india
Reversed string is: aidni

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

Thanks for the program, helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory