void reverse(char *buf)
{
char *start = buf;
char *end = buf + strlen(buf) - 1;
while(start < end)
swap(start++, end--);
}
#include <stdio.h>
#include <string.h>
void swap(char *s1, char *s2)
{
char t = *s1;
*s1 = *s2;
*s2 = t;
}
void swap2(char *s1, char *s2)
{
*s1 ^= *s2;
*s2 ^= *s1;
*s1 ^= *s2;
}
void reverse(char *buf)
{
char *start = buf;
char *end = buf + strlen(buf) - 1;
while(start < end)
swap(start++, end--);
}
int main()
{
char buf[] = "program to reverse a string";
printf("before: %s\n", buf);
reverse(buf);
printf("after : %s\n", buf);
return 0;
}
output:
before: program to reverse a string
after : gnirts a esrever ot margorp
No comments:
Post a Comment