#include<stdio.h>
#include<string.h>
#include<malloc.h>
char *str_rev(char s[]){
static char *st = NULL;
static char *l;
int i = 0,c = 0;
st = malloc((strlen(s) * sizeof(*s))+1);
*l = st;
if(s == NULL){
return "INVALID PARAMS";
}
for(i=0;s[i]!='\0';i++){
;
}
for(c=i;c >=0;c--){
*l++ = s[c];
}
l = '\0';
return st;
}
int main(){
char a[]="Angus Declan R";
printf("\n %s \n",str_rev(a));
return 0;
}
逆の文字列を再実行する必要があるため、func str_rev()でmalloc()を使用して割り当てられたメモリを解放する方法。