Header Ads

Reverse string in C without using strrev() function

It's a C program that explains how to reverse a given string without using the inbuilt function 'strrev()' which is a part of the #include<string.h> header file.

It is very simple to reverse a string by using strrev() function in c:


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

int main(){
  char *str,*str2;
  str = malloc(sizeof(char*));
  str2 = malloc(sizeof(char*));
  scanf("%s",str);
  str2 = strrev(str);
  printf("Reverse of %s is %s",str,str2);
  return 0;
}

Else you can do it as below without using strrev() function:

#include<stdio.h>

int main(){
  char str[100];
  int i=0,n=0;
  scanf("%s",str);

  while(str[i++]!='\0'){
    n++;
  }

  printf("Reverse of %s is ",str);
  for(i=n-1;i>=0;i--){
     printf("%c",str[i]);
  }
  return 0;
}
In both program you get the same output:
dotcprograms.blogspot.in
Reverse of dotcprograms.blogspot.in is ni.topsgolb.smargorpctod

No comments