R
rajash
Thanks for the additional comments.
Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.
#include<stdio.h>
#define SWAP(a,b) { char x; x=(a); (a)=(b); (b)=x; }
void reverse();
main(int argc, char**argv)
{
if(argc>1) {
reverse(argv[1]);
printf("%s\n", argv[1]);
} else
printf("Unspecified error\n");
}
void reverse(char *s)
{
static int n=-1;
if(n==-1)
n=strlen(s)-1;
SWAP(*s, s[n]);
if((n-=2)>=0)
reverse(s+1);
else
n=-1; /* make sure function is reentrant */
}
Here is a solution to an exercise I had problems with. I still don't
think it's really what's wanted as it uses a "state variable" n - but
I can't see how to do it without this (or changing the function to
take an extra argument). Thanks for any hints.
#include<stdio.h>
#define SWAP(a,b) { char x; x=(a); (a)=(b); (b)=x; }
void reverse();
main(int argc, char**argv)
{
if(argc>1) {
reverse(argv[1]);
printf("%s\n", argv[1]);
} else
printf("Unspecified error\n");
}
void reverse(char *s)
{
static int n=-1;
if(n==-1)
n=strlen(s)-1;
SWAP(*s, s[n]);
if((n-=2)>=0)
reverse(s+1);
else
n=-1; /* make sure function is reentrant */
}