Reversing a character string without using library function “strrev()”

This post is in response to my friend Anubhav Singh‘s query regarding how to reverse a character string without using the standard library function strrev(), and is a very common question asked in the technical round for TCS, Wipro, Infosys etc.

Hope this will help!!

/* Webdevil’s BLOG POST */
#include<stdio.h>
#include<string.h>
main()
{
char str[50], revstr[50];  //declare a character array to store the input string and reverse string
int i=0,j=0;
printf(“Enter the string to be reversed : “);
scanf(“%s”,str);
for(i=strlen(str)-1;i>=0;i–)
{
revstr[j]=str[i];
j++;
}
revstr[j]=”;
printf(“Input String : %s”,str);
printf(“\nOutput String : %s”,revstr);

}

The output will be like this ->

Queries are most welcome…!!

Advertisements

7 thoughts on “Reversing a character string without using library function “strrev()”

  1. reversing String recursively:
    # include
    using namespace std;
    void reverse(char *str,int i)
    {
    if(str[i]==”)
    return;
    reverse(str,i+1);
    cout<<str[i];
    }
    int main()
    {
    char str[10];
    cout<>str;
    int i=0;
    reverse(str,i);
    system(“pause”);
    return 0;
    }

    reversing List recursively:

    # include
    using namespace std;
    struct node
    {
    int data;
    node *next;
    };
    void enterdata(node *&head,int data)
    {
    node *newNode = new node;
    newNode->data=data;
    newNode->next=head;
    head=newNode;
    }
    node *reverseList(node *head)
    {
    if(head->next==NULL)
    {
    return NULL;
    }
    node *tmpHead = NULL;
    tmpHead=reverseList(head->next);
    if(tmpHead==NULL)
    {
    tmpHead=head->next;
    tmpHead->next=head;
    head->next=NULL;
    head=tmpHead;
    return head;
    }
    else
    {
    head->next->next=head;
    head->next=NULL;
    head=tmpHead;
    return head;
    }

    }
    void printList(node *head)
    {
    cout<<"\nList is:\n";
    while(head!=NULL)
    {
    cout<data;
    head=head->next;
    }
    }
    int main()
    {
    node *head=NULL;
    for(int i=0;i<10;i++)
    {
    enterdata(head,i);
    }
    printList(head);
    head=reverseList(head);
    printList(head);
    system("pause");
    }

    • gr8 explanation anubhav, thanks for sharing the info… 🙂
      i am working on PHP to generate a function prototype that can reverse a single linked list..
      Hope to finish it soon..

    • <?php
      /* this is the prototype of how php can reverse a single-linked list */
      /* Webdevil’s BLOG POST */
      class WordNode
      {
      public $word;
      public $nextNode;

      public function __construct($word = null)
      {
      $this->word = $word;
      }

      // this function prints all content of this node, and all the linked nodes
      public function __toString($separator = ”)
      {
      $buffer = ”;
      $current = $this;

      do
      {

      $buffer .= $current->word . (($current->nextNode !== null) ? $separator : ”);

      }
      while(($current = $current->nextNode) !== null);

      return $buffer;
      }
      }

      // Take an array and turn it into a linked list

      function buildList($sourceArray)
      {
      // First step create the linked list
      $firstNode = new WordNode($sourceArray[0]);

      $lastNode = $firstNode;

      foreach(array_slice($sourceArray, 1) as $word)
      {
      $thisNode = new WordNode($word);

      $lastNode->nextNode = $thisNode;

      $lastNode = $thisNode;
      }

      return $firstNode;
      }

      function reverseList(WordNode &$head)
      {
      $tail = $head->nextNode;

      $head->nextNode = null;

      while($tail !== null)
      {

      $next = $tail;
      $tail = $next->nextNode;

      $next->nextNode = $head;

      $head = $next;
      }
      }

      $list = buildList(array(“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”, “i”, “j”, “k”));

      echo $list . “\n”;

      reverseList($list);

      echo $list . “\n”;

      reverseList($list);

      echo $list . “\n”;

      echo $list->__toString(” “) . “\n”;

      ?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s