Feb 182016
 

In C language, string library has a function strcmp that takes two arguments and return -1, 0 or 1 depending on whether first string is less than equal-to or greater than the second string.

 
int strcmp(const char *str1, const char *str2);

Write similar function that compare strings given in the form of linked list, For example:

If:
  First List:  s->a->n->s->k->r->i->t
  Second List: s->a->n->s->k->r->i->t->i
Output: -1

If:
  First List:  s->a->n->s->k->r->i->t
  Second List: a->t->m->a->n
Output: 1

If:
  First List:  n->o->n->t->r->a->n->s->l->a->t->a->b->l->e
  Second List:  n->o->n->t->r->a->n->s->l->a->t->a->b->l->e
Output: 0

The signature of function will change like as shown below:

 
int strcmp(Node *head1, Node *head2);

Continue reading »

Aug 072012
 

We have already seen the problem to reverse the words in a string, in the solution to that problem we have also written function to reverse the string. That was a non-recursive (iterative) function. Write a recursive function to reverse the String.

For example: If the String is “ABCD” then the output should be “DCBA“.

Before getting to the solution, please note that a non-recursive solution is always better than a recursive solution in terms of its time and space complexities. To know more about recursion refer this post.
Continue reading »