Home » C Programs » Program to Reverse a String in C

Program to Reverse a String in C

Program to reverse a string in c. We will learn how to reverse a given String in this lesson. The output should be “plppa” if the string is “apple.” This notion can be used to check the palindrome. Because the value of the palindrome string remains the same even after it is reversed. In C, a string (sometimes referred to as a C string) is an array of characters followed by a NULL character. A set of characters is contained within double quotations to represent a string (“).

String in C Program

A string is a data type similar to an integer and a floating-point unit that is used to represent text rather than numbers in programming. The words “food” and “I have 3 food,” for example, are both strings. If provided correctly, even “12345” may be deemed a string. Here we have executed the program to reverse a string in c

How to Calculate String Length in C

The number of characters typed in the String is returned by the string length method. This function calculates the length of any string by counting the number of 16-bit Unicode characters in the string. 

In the C programming language, a string is an array of characters that ends with a null character (\0). The length of a string is the number of characters in it. A character is not counted when the string length is ‘0.’ 

Post Increment in C

Post-increment (i++) The value is incremented after it has been assigned to the variable. The value is first used in an expression and then incremented in the Post-Increment. Syntax: a = x++; If the value of ‘x’ is 10, then the value of variable ‘a’ will also be 10 because the old value of ‘x’ is used. 

strrev Function in C Program

The strrev() function is specified in the string. h header file and is a built-in function in C. To reverse the given string, use the strrev() method. char *strrev(char *str);. 

Program to Reverse a String in C

//program to reverse a string in c
#include <stdio.h>
int main()
{
char str[1000], rev[1000];
int i, j, value = 0;
scanf("%s", str);
printf("\nReverse the string %s", str);
while (str[value] != '\0')
{
value++;
}
j = value - 1;
for (i = 0; i < value; i++)
{
rev[i] = str[j];
j--;
}
printf("\nString after reversed %s", rev);
}

Output

inlarn

Reverse the string inlarn
String after reversed nralni