I have this code, which doubles every letter in nabucco
:
#include<stdio.h>
#include<string.h>
int main(){
char string1[20] = "nabucco";
char string2[40];
int j=-1;
for(int i=0; i<strlen(string1);i++){
j++;
string2[j] = string1[i];
j++;
string2[j] = string1[i];
}
printf("string1=\t%s\n",string1);
printf("string2=\t%s\n",string2);
}
string2
is set to and prints nnaabbuuccccoo
.
However, when I try to set j=0;
string2
prints (and is presumably set to) nothing.
If j=0
before going into the loop, then j is incremented to 1 in the for
loop, which starts off setting the second element of the empty string2
. Is there something behind the scenes preventing any element other than the first from being set? I do not understand what is causing this behavior.