L
lovecreatesbea...
/*reverse a string, e.g. from "abc" to "cba". no library function
invoking is allowed.*/
void reverse(char *s){
char *p = s;
char c;
int n1 = 0, n2 = 0;
while (*p++)
n2++;
n2--;
while (n1 < n2){
c = s[n1];
s[n1] = s[n2];
s[n2] = c;
n1++;
n2--;
}
}
void reverse2(char *s){
char *p1 = s, *p2 = s;
char c;
while (*p2)
p2++;
p2--;
while (p1 < p2){
c = *p1;
*p1 = *p2;
*p2 = c;
p1++;
p2--;
}
}
I have written two versions of reverse function. How do you think about
them? Can they be shorter or more efficient.
invoking is allowed.*/
void reverse(char *s){
char *p = s;
char c;
int n1 = 0, n2 = 0;
while (*p++)
n2++;
n2--;
while (n1 < n2){
c = s[n1];
s[n1] = s[n2];
s[n2] = c;
n1++;
n2--;
}
}
void reverse2(char *s){
char *p1 = s, *p2 = s;
char c;
while (*p2)
p2++;
p2--;
while (p1 < p2){
c = *p1;
*p1 = *p2;
*p2 = c;
p1++;
p2--;
}
}
I have written two versions of reverse function. How do you think about
them? Can they be shorter or more efficient.