Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Friday, May 30, 2014

Reverse string

C programming code

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
   char arr[100];
   clrscr();
   printf("Enter a string to reverse\n");
   gets(arr);
   strrev(arr);
   printf("Reverse of entered string is \n%s\n",arr);
   getch();
}

C program to reverse a string using pointers

 Now we will invert string using pointers or without using library function strrev.
#include<stdio.h>
#include<conio.h> 
int string_length(char*);
void reverse(char*);
void main() 
{
   char string[100];
   clrscr(); 
   printf("Enter a string\n");
   gets(string);
   reverse(string);
   printf("Reverse of entered string is \"%s\".\n", string);
   getch();
}
void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;
   length = string_length(string);
   begin = string;
   end = string;
    for ( c = 0 ; c < ( length - 1 ) ; c++ )
      end++;
    for ( c = 0 ; c < length/2 ; c++ ) 
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;
      begin++;
      end--;
   }
}
int string_length(char *pointer)
{
   int c = 0;
   while( *(pointer+c) != '\0' )
    c++;
    return c;
}

C program to reverse a string using recursion

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void reverse(char*, int, int);
void main()
{
   char a[100];
   gets(a);
   reverse(a, 0, strlen(a)-1);
   printf("%s\n",a);
   getch();
}
void reverse(char *x, int begin, int end)
{
   char c;
   if (begin >= end)
   return;
   c = *(x+begin);
   *(x+begin) = *(x+end);
   *(x+end) = c;
   reverse(x, ++begin, --end);
}

No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC