Min Ko Ko Min Ko Ko - 1 year ago 67
C Question

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[];

return 0;

Answer Source

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.