Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Strings
  6. String Reverse

String Reverse

strrev()

Reverses a given string

char *strrev(char *string);

Program: Reversing a string without using string functions

#include<stdio.h>
#include<string.h>
int main()
{
	char s[100];
	puts("Enter string 1");
	gets(s);
	strrev(s);
	puts("Reversed string is");
	puts(s);
	return 0;
}

Output

Enter string 1
bag
Reversed string is
Gab

Program: Reversing a string without using string functions

#include<stdio.h>
int main()
{
	char s[100],r[100];
	int i=0,j=0;
	puts("Enter string");
	gets(s);
	while(s[i] != '\0')
	{
		i++;
	}
	while(i>0)
	{
		i--;
		r[j] = s[i];
		j++;
	}
	r[j] = '\0';
	puts("Reversed string is ");
	puts(r);
	return 0;
}

Output

Enter string
bag
Reversed string is
Gab

Program: Reversing a string without using string functions

#include<stdio.h>
int main()
{
	char s[100],r[100];
	int i=0,j=0;
	puts("Enter string");
	gets(s);
	while(s[i] != '\0')
	{
		i++;
	}
	while(i>0)
	{
		i--;
		r[j] = s[i];
		j++;
	}
	r[j] = '\0';
	puts("Reversed string is ");
	puts(r);
	return 0;
}

Output

Enter string
bag
Reversed string is
Gab

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments