I have this simple program in which I initialize a string with "HELLO". I need the output to be printed as HLOEL, i.e) all the even indexes (0,2,4) followed by the odd ones (1,2). I could not infer what's wrong with my code, but it yields "HLOELHLO" instead of "HLOEL". Could someone explain what is happening here?
#include <stdio.h> int main() { int i,loPos=0,hiPos=0; char *str="HELLO"; char lo[2]; char hi[3]; for(i=0;i<5;i++) { if(i%2==0) { hi[hiPos++]=str[i]; } else { lo[loPos++]=str[i]; } } printf("%s%s",hi,lo); return 0; } Thanks in Advance!
\0as the last element.char lo[16] = "";, which will incidentally make sure it's zero-initialized (zero is the same as the string null terminator))."HELLO"are really read only arrays of characters (including the null terminator). Because they are read only (they are not allowed to be modified) it's recommended to useconst char *for pointers to them.