×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Initial of the name - String Accept a String and Reverse Each Word of it - String
Accept a String and Reverse It - String - String - C Language
1926    Arnab De    26/05/2019

We can reverse a string in two different ways (1) using a second character array or string (2) without using a second character array or string.

Using a second character array or string

Read a string from user using gets () function. Now travel the whole string from the end in front and store each character in a another string. Now terminate the new string with a null character ('\0').

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
	char str[30],rev[30];
	int j,i,l;
	printf("Enter A String: ");
	gets(str);
	l=strlen(str);
	for(i=0,j=l-1;j>=0;i++,j--)
	{
		rev[i]=str[j];
	}
	rev[i]='\0';
	printf("Reverse String Is ");
	puts(rev);
	return 0;
}
softetechnologies

Without using a second character array or string

Read a string from user using gets () function. Now travel the string from the both sides of the string and exchange the corresponding characters until the position of both sides equal or overlapping.

softetechnologies
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
	char str[30],ch;
	int j,i,l;
	printf("Enter A String: ");
	gets(str);
	l=strlen(str);
	for(i=0,j=l-1;i<j;i++,j--)
	{
		ch=str[i];
		str[i]=str[j];
		str[j]=ch;
	}
	
	printf("Reverse String Is ");
	puts(str);
	return 0;
}
Initial of the name - String Accept a String and Reverse Each Word of it - String
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Fill the brain with high thoughts, highest idrals, place them day and night before you, and out of that will come great work.
Swami Vivekananda
9653
72.01
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     51817
25/06/2018     43664
01/01/2018     42781
28/06/2017     40509
02/08/2017     39329
01/08/2017     33541
06/07/2017     33294
15/05/2017     32656
14/07/2017     28867
11/09/2018     28672
softetechnologies