I wrote a loop to replace every character in a string with another using a reference array.
for(int i=0 ; i < encoded_message_len ; i++){
for(int j=0; j < 26 ; j++){
if(encoded_message_copy[i] == substitution_alphabet[j]){
printf("%c ->>>> %c @ index:%d \n",encoded_message_copy[i], original_alphabet[j], i);
encoded_message_copy[i] = original_alphabet[j];
}
}
}
When I run this code however, I get a strange output:
J ->>>> C @ index:0
H ->>>> R @ index:1
Q ->>>> Y @ index:2
Y ->>>> Z @ index:2
S ->>>> P @ index:3
U ->>>> T @ index:4
T ->>>> V @ index:4
X ->>>> O @ index:6
F ->>>> L @ index:7
L ->>>> X @ index:7
X ->>>> O @ index:8
B ->>>> G @ index:9
G ->>>> W @ index:9
Q ->>>> Y @ index:10
Y ->>>> Z @ index:10
When I remove this line : encoded_message_copy[i] = original_alphabet[j]; from the loop, I get the expected output:
J ->>>> C @ index:0
H ->>>> R @ index:1
Q ->>>> Y @ index:2
S ->>>> P @ index:3
U ->>>> T @ index:4
X ->>>> O @ index:6
F ->>>> L @ index:7
X ->>>> O @ index:8
B ->>>> G @ index:9
Q ->>>> Y @ index:10
Can someone explain why this is happening?
substitution_alphabet, and substituted again. Put abreakstatement at the end of the body of yourifstatement.