Search This Blog

C Program to Reverse String Without Using Library Function

??Source code:
 #include <stdio.h>  
 #include<string.h>  
 int main()  
 {  
   char str[20];  
   char r1str[20];  
   char r2str[20];  
   int len;  
   int c;  
   printf("Input a string\n");  
   gets(str);  
   c = 0;  
  while (str[c] != '\0')  
    c++;  
  len=c;  
  c=0;  
 printf("Length of \"%s\" = %d\n", str, len);  
   while (len>0)  
   {  
    r1str[c] = str[len-1];  
    len--;  
    c++;  
   }  
   r1str[c]='\0';  
   strcpy(r2str,strrev(str));  
   printf("\n Reverse of string without using library function is : %s",r1str);  
   printf("\n Reverse of string by using library function is : %s",r2str);  
  return 0;  
 }  

OUTPUT:

Program : Reverse String Without Using Library Function

#include
#include
int main() {
   char str[100], temp;
   int i, j = 0;
   printf("\nEnter the string :");
   gets(str);
   i = 0;
   j = strlen(str) - 1;
   while (i < j) {
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
   }
   printf("\nReverse string is :%s", str);
   return (0);
}
OUTPUT:
Enter the string :rihana
Reverse string is :anahir

No comments:

Post a Comment

leave a comment here