Trying to solve a problem on projectlovelace.net. I can't figure out why I can't assign values through a pointer as if it were an array. I thought that was possible.
It's been a while since I coded in C, so I'm sure there's a bone-headed obvious reason. (The problem requires me to both return the string and change the string, in case you wondered.)
#include <stdio.h>
#include <string.h>
char* rna(char* dna) {
size_t i, len = strlen(dna);
char rna[len + 1];
rna[len] = '\0';
for(i = 0; i < len; i++){
char letter = dna[len - 1 - i];
if(letter == 'T')
letter = 'U';
rna[i] = letter;
}
// problem is here.
for(i = 0; i < len; i++)
dna[i] = rna[i];
return dna;
}
int main(void){
char *dna = "CCTAGGACCAGGTT";
printf("Input DNA: %s\nOutput RNA: %s\n", dna, rna(dna));
return 0;
}
Even using dna[0] = 'X'
causes a segfault.