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 » Pointers » C program to reverse the given string using a pointer
Suryateja Pericherla Categories: Pointers. No Comments on C program to reverse the given string using a pointer
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

In this article we will learn to implement a C program to reverse the given string using a pointer. A C program is provided below for reversing a given string using a pointer.

 

Program is as follows:

/*
 * C program to reverse a given string using pointers
 * Author: P.S.SuryaTeja
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

void reverse(char *str)
{
	char *first, *last, temp;;
	int i, stringlength;
	first = str;
	stringlength = strlen(str);
	for(i = 0; i < stringlength - 1; i++)
		str++;
	last = str;
	str = first;
	while(first < last)
	{
		temp = *first;
		*first = *last;
		*last = temp;
		first++;
		last--;
	}
	puts(str);
}

int main(int argc, char **argv)
{
	char str[20];
	printf("Enter a string: ");
	gets(str);
	printf("Reverse of the string is: ");
	reverse(str);
    getch();
    return 0;
}

 

Input and output for the above program is as follows:

Enter a string: howdy
Reverse of the string is: ydwoh

 

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?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory