R
Rakesh
Hi,
I have this function to reverse the given string.
I am just curious if that is correct and there could be better way of
doing it / probable bugs in the same.
The function prototype is similar to the one in any standard C
library.
<---- Code starts -->
char * my_strrev(char * mine) {
static char * p = NULL; // To make sure that the pointer is not
lost on return.
char * q;
int len;
int i;
len = strlen(mine);
p = new char[len + 1];
// 1 for '\0' character
q = mine;
i = len - 1;
while (*q) {
*(p + i) = *q++;
i--;
}
*(p + len ) = '\0';
return p;
}
<-- Code Ends -->
- Rakesh.
I have this function to reverse the given string.
I am just curious if that is correct and there could be better way of
doing it / probable bugs in the same.
The function prototype is similar to the one in any standard C
library.
<---- Code starts -->
char * my_strrev(char * mine) {
static char * p = NULL; // To make sure that the pointer is not
lost on return.
char * q;
int len;
int i;
len = strlen(mine);
p = new char[len + 1];
// 1 for '\0' character
q = mine;
i = len - 1;
while (*q) {
*(p + i) = *q++;
i--;
}
*(p + len ) = '\0';
return p;
}
<-- Code Ends -->
- Rakesh.