#include <stdio.h>
#include <string.h>
int copylen(char *a);
char reverse(char *s, char *t, int size);
int main()
{
char source[25]; /* source string */
char target[25]; /* Target string */
printf("Enter the source string\n");
scanf("%s",source);
/* function which passes the base address of source and target */
reverse(source, target, sizeof(target));
printf("The target string is %s", target);
getch();
}
char reverse(char *s, char *t, int size) /* Pointers to source and target */
{
int length=0;
int count=0;
/* The length of the source string is calculated and then assigned to length using copylen*/
length=copylen(s)-1;
while(length>=0&&count<size-1)
{
/* the last charachter s[length] or *(s+length) is copied to the target */
*t=*(s+length);
count++;
/* The pointer is made to point to the next memory address of the target */
t++;
/* length is reduced so the the next charachter is s[length-1] */
length--;
}
*t='\0'; /* The terminating charchter is assigned to the end of the target */
}
int copylen(char *a)
{
int len=0; /* Initialize length variable */
while(*a!='\0') /* Till termination charachter is met continue the loop */
{
len++; /* Increase length */
a++; /* shift the pointer to the next location */
}
return len; /* The length which will be the length of the string */
}