Tuesday, July 26, 2011

String reversal in C

  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: 
  4: int mystrrev(char * str,int size);
  5: 
  6: int main()
  7: {
  8:  char name1[7]="Mithun";
  9:  char name2[6]="Mithu";
 10:  mystrrev(&name1,6);
 11:  mystrrev(&name2,5);
 12: 
 13:  printf("%s \n",name1);
 14:  printf("%s \n",name2);
 15: 
 16: }
 17: 
 18: int mystrrev(char * str,int size)
 19: {
 20:  int pivot = size/2;
 21:  char* s = str;
 22:  char* e = str+size-1;
 23:  printf("%d, %c \n", *s,*e);
 24: 
 25:  while(*str)
 26:  {
 27:   printf("%c , %d \n",*str,*str);
 28:   str++;
 29:  }
 30: 
 31:  while(pivot--)
 32:  {
 33:   *s = (*s)+(*e);
 34:   *e = (*s)-(*e);
 35:   *s = (*s)-(*e);
 36: 
 37:   s++;
 38:   e--;
 39:  }
 40: }
 

No comments: