📜  用于查找字符串中字符的 ASCII 值的异或的程序

📅  最后修改于: 2021-09-04 08:36:26             🧑  作者: Mango

给定一个字符串str ,任务是找到字符串中字符的 ASCII 值的异或。
例子:

做法:思路是将每个字符的ASCII值一一找出,并求出这些值的异或值。
下面是上述方法的实现:

C++
// C++ program to find XOR of ASCII
// value of characters in string
 
#include 
using namespace std;
 
// Function to find the XOR of ASCII
// value of characters in string
int 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 code
int 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
// Java program to find XOR of ASCII
// value of characters in String
class GFG{
  
// Function to find the XOR of ASCII
// value of characters in String
static 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 code
public 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
# Python3 program to find XOR of ASCII
# value of characters in str1ing
 
# Function to find the XOR of ASCII
# value of characters in str1ing
def XorAscii(str1, len1):
 
    # store value of first character
    ans = ord(str1[0])
 
    for i in range(1,len1):
 
        # Traverse str1ing to find the XOR
        ans = (ans ^ (ord(str1[i])))
 
    # Return the XOR
    return ans
 
# Driver code
str1 = "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#
// C# program to find XOR of ASCII
// value of characters in String
using System;
 
class GFG{
   
// Function to find the XOR of ASCII
// value of characters in String
static 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 code
public 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


Javascript


输出:

123
102

时间复杂度: O(N) ,其中 N 是字符串的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live