#include <stdio.h>
#include <string.h>
int main()
{
char greeting[]="\nHello World!\n";
int a;
for(int i=0; i<strlen(greeting); i++)
greeting[i]^=111;
for(int i=0; i<strlen(greeting); i++)
greeting[i]^=111;
printf("%s\n",greeting);
scanf("%d",&a);
}
Output:
Hell
Why does it cut everything after spotting a letter corresponding to the XOR key’s number (in this case, ASCII ‘w’)? In mathematical logic, N^N=0 and 0^N=N, doesn’t it?
Because
'o'is ASCII code 111, and XORing 111 with 111 yields 0,NUL, and terminates your string. Once this happens (even in the first loop, since you’re evaluating it each time through the loop),strlenreports the string is much shorter, and the loops stop.Saving the string length before going through the XORs will save you from this.