Monday, 8 December 2014

C Program to reverse a string by using functions

Program to reverse string
#include<stdio.h>
#include<string.h>
char* myrev(char *);
main()
{
char str[50];
printf("enter a string\n");
scanf("%[^\n]s",str);
//n=strlen(str);
myrev(str);
printf("%s",str);
}
char* myrev(char* s1)
{
int n,i,j;
char temp;
n=strlen(s1);
for(i=0,j=n-1;i<j;i++,j--)
{
temp=s1[i];
s1[i]=s1[j];
s1[j]=temp;
}
return(s1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array12.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
emoclew