Program to find the XOR of ASCII values of characters in a string - GeeksforGeeks

28 Jan, 2022
Given a string str, the task is to find the XOR of ASCII values of characters in the string.Examples:  
Input: str = “Geeks” Output: 95 ASCII value of G = 71 ASCII value of e = 101 ASCII value of e = 101 ASCII value of k = 107 ASCII value of s = 115 XOR of ASCII values = 71 ^ 101 ^ 101 ^ 107 ^ 115 = 95Input: str = “GfG” Output: 102  
 
Approach: The idea is to find out the ASCII value of each character one by one and find the XOR value of these values.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to find XOR of ASCII// value of characters in string #include <bits/stdc++.h>using namespace std; // Function to find the XOR of ASCII// value of characters in stringint XorAscii(string str, int len){     // store value of first character    int ans = int(str[0]);     for (int i = 1; i < len; i++) {         // Traverse string to find the XOR        ans = (ans ^ (int(str[i])));    }     // Return the XOR    return ans;} // Driver codeint main(){     string str = "geeksforgeeks";    int len = str.length();    cout << XorAscii(str, len) << endl;     str = "GfG";    len = str.length();    cout << XorAscii(str, len);     return 0;}
// Java program to find XOR of ASCII// value of characters in Stringclass GFG{  // Function to find the XOR of ASCII// value of characters in Stringstatic int XorAscii(String str, int len){      // store value of first character    int ans = (str.charAt(0));      for (int i = 1; i < len; i++) {          // Traverse String to find the XOR        ans = (ans ^ ((str.charAt(i))));    }      // Return the XOR    return ans;}  // Driver codepublic static void main(String[] args){      String str = "geeksforgeeks";    int len = str.length();    System.out.print(XorAscii(str, len) +"\n");      str = "GfG";    len = str.length();    System.out.print(XorAscii(str, len));}} // This code is contributed by PrinciRaj1992
# Python3 program to find XOR of ASCII# value of characters in string # Function to find the XOR of ASCII# value of characters in stringdef XorAscii(str1, len1):     # store value of first character    ans = ord(str1[0])     for i in range(1,len1):         # Traverse string to find the XOR        ans = (ans ^ (ord(str1[i])))     # Return the XOR    return ans # Driver codestr1 = "geeksforgeeks"len1 = len(str1)print(XorAscii(str1, len1)) str1 = "GfG"len1 = len(str1)print(XorAscii(str1, len1)) # This code is contributed by mohit kumar 29
// C# program to find XOR of ASCII// value of characters in Stringusing System; class GFG{   // Function to find the XOR of ASCII// value of characters in Stringstatic int XorAscii(String str, int len){       // store value of first character    int ans = (str[0]);       for (int i = 1; i < len; i++) {           // Traverse String to find the XOR        ans = (ans ^ ((str[i])));    }       // Return the XOR    return ans;}   // Driver codepublic static void Main(String[] args){       String str = "geeksforgeeks";    int len = str.Length;    Console.Write(XorAscii(str, len) +"\n");       str = "GfG";    len = str.Length;    Console.Write(XorAscii(str, len));}} // This code is contributed by PrinciRaj1992
<script> // Javascript program to find XOR of ASCII// value of characters in string // Function to find the XOR of ASCII// value of characters in stringfunction XorAscii(str, len){     // store value of first character    let ans = str.codePointAt(0);     for (let i = 1; i < len; i++) {         // Traverse string to find the XOR        ans = (ans ^ (str.codePointAt(i)));    }     // Return the XOR    return ans;} // Driver code     let str = "geeksforgeeks";    let len = str.length;    document.write(XorAscii(str, len) + "<br>");     str = "GfG";    len = str.length;    document.write(XorAscii(str, len)); </script>
123
102
 
Time Complexity: O(N), where N is the length of string. 
mohit kumar 29
princiraj1992
souravmahato348
simmytarika5
Bitwise-XOR
C-String-Question
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not