简体   繁体   中英

How to xor two string in C?

I have 2 string value. str and str2 . i wish to xor the str and str2

My code is

#include <stdio.h>

int main(){
    char str[]   =  "Hello";
    char str2[]  =  "World";
    char outupt;

    output = str[] ^ str2[]; 
    printf("%s",output)   

    return 0;
}

character by character:

#include <stdio.h>
#include <string.h>

int main(){
  int i;
  char str[]   =  "Hello";
  char str2[]  =  "World";
  char output[6];

  for (i=0; i<strlen(str); i++)
  {
    char temp = str[i] ^ str2[i];
    output[i] = temp;
  }

  output[i] = '\0';
  printf("%s", output);

  return 0;
}

Of course you'll need to make sure output is large enough to hold the result (including the null terminator) and you'll need to decide what to do if str and str2 aren't the same length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM