AD1

Thursday, 2 February 2017

reverse a string

Write a function reverse() that accepts a string and reverses it 
as example reverse("hello") would return "olleh"
#include <stdio.h>
#include <string.h>
char* reverse(char *base)// passing array to a function that returns pointer to character
{
int i=0;
int j=strlen(base)-1;
char temp;
while(i!=j)
{
temp=base[i];
base[i]=base[j];
base[j]=temp;
i++;
j--;
}
return base;
}
int main()
{
char array[5]="hello";
puts(reverse(array));
return 0;
}
view raw reverse.c hosted with ❤ by GitHub

No comments:

Post a Comment