C program to reverse the string.

Posted by Mangesh on April 01, 2018

/* Reverse a string. */

Description :

We will be given a string and we must reverse it.

Logic :

We shall replace the first character with the last character, second character with second last character, and so on. We shall continue this till floor(n/2).

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,length,loop;
  char string[100], temp;
  printf("Enter the String : ");
  gets(string);
  length = strlen(string);
  loop = length/2;
  for (i=0;i<loop;i++)
  {
    temp = string[i];
    string[i] = string[length-i-1];
    string[length-i-1] = temp;
  }
  printf("\nThe reverse value of string is %s",string);
  getch();
}

Output :

c program for reverse string Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for naive string matching algorithm.
2 C program to check whether the given string is palindrome or not.
3 C program to concatenate two string.
4 C program to check whether two strings are anagram or not.
5 C program to reverse the string.
6 C program to replace a character in a string.
7 C program to compare two string.
8 C program to compare two string using strcmp function.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.