inverting a string of bits in C but when I return it back to the main function it comes out as random characters.. what should I do?
const char * invert1(char S_string[]) {
char new_S[strlen(S_string)]; //malloc(strlen(S_string)*sizeof(int));
char *ptr = new_S;
printf("S_string = %s\n", S_string);
int i = 0;
for(i = 0; i < strlen(S_string); i++) {
if(S_string[i] == 48){
new_S[i] = 49;
}
else if(S_string[i] == 49){
new_S[i] = 48;
}
else{
return "354";
}
printf("1new_S = %s\n", new_S);
printf("i = %d\n", i);
}
printf("%s\n", new_S);
printf("%s", ptr);
printf("%s", "yeup");
return ptr;
}
int main(int n, int k){
char S[] = "1";
//char temp[strlen(S)] = invert1(S);
printf("%s", invert1(S));
}
This is the output:
S_string = 1
1new_S = 0
i = 0
0
0yeup v~v�S��@
After "yeup", it should print "0" What am I doing wrong?