Task 1: Reverse a character string.
Write a function called str_rev() that takes a pointer to a string as input and reverses the string in-place (this change should be reflected in the original string that was passed to the function, not to a copy of the string). No library functions for string reversal should be used. The function should also return a pointer to the reversed string.
Function prototype is given below:
char * str_rev(char * str);
#include <stdio.h>
char* str_rev(char* str)
{
  char *first, *last;
  first = last = str;
  while(*last != 0)
  {
    ++last;
  } Â
  while(str < --last)
  {
    char tmp;
    tmp = *str;
    *str = *last;
    *last = tmp;
    ++str;
    --last;
  }
  Â
  return first;
}
int main()
{
  char* test_string = "12345";
  printf("Reversed string is '%s'\n", str_rev(test_string));
  return 0;
}
Comments
Leave a comment