异或"的Hello World&QUOT!;切断串 [英] XORing "Hello World!" cuts off string

查看:101
本文介绍了异或"的Hello World&QUOT!;切断串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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);

}

输出:

Hell

为什么它切一切斑点对应于XOR键的数目的信后(在此情况下,ASCII'W')?在数理逻辑, N R个N = 0 0 ^ N = N ,不是吗?

推荐答案

由于'O'是ASCII code 111和异或111与111的产量为0, NUL ,并终止您的字符串。一旦发生这种情况(即使在第一循环中,因为你通过循环每次评测吧),的strlen 报告的字符串是要短得多,而循环停止。

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), strlen reports the string is much shorter, and the loops stop.

在经历了异或将节省你从这个保存字符串的长度。

Saving the string length before going through the XORs will save you from this.

这篇关于异或&QUOT;的Hello World&QUOT!;切断串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆