String Reverse – STRREV

String Reverse - STRREV

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C programming concepts. You can learn C language at your own speed and time. One can learn concepts of C language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

String Reverse - STRREV
String Reverse – STRREV

Write a C program to String Reverse – STRREV

PROGRAM: String Reverse – STRREV

/* STRING REVERSE - STRREV */

#include<stdio.h>

int main()
{
 char str[100];
 int i,j;
 char c;
 printf("Please enter a string: ");
 // gets(str); 
 // fgets is a better option over gets to read multiword string .
 fgets(str, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;
   
 for(i=0;str[i];i++);
 for(j=0,i--;j<i;j++,i--)
 {
  c=str[i];
  str[i]=str[j];
  str[j]=c;
 }

 printf("Reversed string is: %s",str);

 return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *