@vnp @vnp is absolutely right on all counts.
In addition,
since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
It's inconvenient and error prone for callers to set the end pointer.
I recommend a wrapper function that takes a single char*
parameter,
and sets the end pointer correctly, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
To make the program easier to play around with,
I recommend to make the main
function work with command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
Finally, in the question you described the task as reversing a string, but called the method "revert" instead of "reverse". The latter would be better, as "reverting" means more like "undoing".
@vnp is absolutely right on all counts.
In addition,
since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
It's inconvenient and error prone for callers to set the end pointer.
I recommend a wrapper function that takes a single char*
parameter,
and sets the end pointer correctly, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
To make the program easier to play around with,
I recommend to make the main
function work with command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
Finally, in the question you described the task as reversing a string, but called the method "revert" instead of "reverse". The latter would be better, as "reverting" means more like "undoing".
@vnp is absolutely right on all counts.
In addition,
since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
It's inconvenient and error prone for callers to set the end pointer.
I recommend a wrapper function that takes a single char*
parameter,
and sets the end pointer correctly, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
To make the program easier to play around with,
I recommend to make the main
function work with command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
Finally, in the question you described the task as reversing a string, but called the method "revert" instead of "reverse". The latter would be better, as "reverting" means more like "undoing".
@vnp is absolutely right on all counts.
Since
In addition,
since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
Also, it'sIt's inconvenient and error prone for a callercallers to set the start and end pointerspointer.
I would createrecommend a wrapper function that takes a single char*
and doesparameter,
and sets the right thingend pointer correctly, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
And toTo make thisthe program easier to play around with,
I wouldrecommend to make the main
function takework with command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
Finally, in the question you described the task as reversing a string, but called the method "revert" instead of "reverse". The latter would be better, as "reverting" means more like "undoing".
@vnp is absolutely right on all counts.
Since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
Also, it's inconvenient for a caller to set the start and end pointers.
I would create a wrapper function that takes a single char*
and does the right thing, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
And to make this easier to play with,
I would make the main
function take command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
@vnp is absolutely right on all counts.
In addition,
since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
It's inconvenient and error prone for callers to set the end pointer.
I recommend a wrapper function that takes a single char*
parameter,
and sets the end pointer correctly, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
To make the program easier to play around with,
I recommend to make the main
function work with command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}
Finally, in the question you described the task as reversing a string, but called the method "revert" instead of "reverse". The latter would be better, as "reverting" means more like "undoing".
@vnp is absolutely right on all counts.
Since the updated values of ++left
and --right
are not needed,
I would prefer to use left + 1
and right - 1
.
Also, it's inconvenient for a caller to set the start and end pointers.
I would create a wrapper function that takes a single char*
and does the right thing, for example:
void revert_recursive(char* left, char* right) {
if (left < right) {
char tmp = *right;
*right = *left;
*left = tmp;
revert_recursive(left + 1, right - 1);
}
}
void revert(char* str) {
revert_recursive(str, str + strlen(str) - 1);
}
And to make this easier to play with,
I would make the main
function take command line arguments:
int main(int argc, char ** argv) {
int i;
char* arg;
for (i = 1; i < argc; ++i) {
arg = argv[i];
printf("%s\n", arg);
revert(arg);
printf("%s\n", arg);
}
return 0;
}