# Calculate the Hamming distance between two strings

Here given code implementation process.

``````// C Program
// Calculate the Hamming distance between two strings
#include <stdio.h>
#include <string.h>

// Calculate hamming distance
void hammingDistance(char *str1,char *str2)
{
// Get the length of string element
int n1 = strlen(str1);
int n2 = strlen(str2);

if(n1 != n2)
{
// When given string size are not equals
printf(" String (%s,%s) are not equals length\n",str1,str2);
}
else
{
// Used to store result
int result = 0;
// Loop controlling variable
int i = 0;
// Compare string element of str1 and str2
while( i < n1)
{
if(str1[i] != str2[i])
{
// When element of (i) location are not same
result++;
}
i++;
}
// Display calculated result
printf("Hamming Distance of String (%s,%s) is : %d\n",str1,str2,result);
}
}
int main()
{

char str1[] = "100101";
char str2[] = "111001";
char str3[] = "pineapple";
char str4[] = "pinxxcode";
char str5[] = "abc";
char str6[] = "abc";

// Test Cases
hammingDistance(str1,str2);
hammingDistance(str3,str4);
hammingDistance(str5,str6);
return 0;
}``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````/*
Java Program
Calculate the Hamming distance between two strings
*/

public class Calculation
{

// Calculate hamming distance
public void hammingDistance(String str1, String str2)
{
// Get the length of string element
int n1 = str1.length();
int n2 = str2.length();
if (n1 != n2)
{
// When given string size are not equals
System.out.print(" String (" + str1 + "," + str2 + ") are not equals length\n");
}
else
{
// Used to store result
int result = 0;
// Loop controlling variable
int i = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (str1.charAt(i) != str2.charAt(i))
{
// When element of (i) location are not same
result++;
}
i++;
}
// Display calculated result
System.out.print("Hamming Distance of String (" + str1 + "," + str2 + ") is : " + result + "\n");
}
}
public static void main(String[] args) {

Calculation obj = new Calculation();

String str1 = "100101";
String str2 = "111001";
String str3 = "pineapple";
String str4 = "pinxxcode";
String str5 = "abc";
String str6 = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);

}
}
``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````// Include header file
#include <iostream>

#include<string.h>

using namespace std;
/*
C++ Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
public:
// Calculate hamming distance
void hammingDistance(string str1, string str2)
{
// Get the length of string element
int n1 = str1.size();
int n2 = str2.size();

if (n1 != n2)
{
// When given string size are not equals
cout << " String (" << str1 << "," << str2 << ") are not equals length\n";
}
else
{
// Used to store result
int result = 0;
// Loop controlling variable
int i = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (str1[i] != str2[i])
{
// When element of (i) location are not same
result++;
}
i++;
}
// Display calculated result
cout << "Hamming Distance of String (" << str1 << "," << str2 << ") is : " << result << "\n";
}
}
};
int main()
{
Calculation obj = Calculation();
string str1 = "100101";
string str2 = "111001";
string str3 = "pineapple";
string str4 = "pinxxcode";
string str5 = "abc";
string str6 = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
return 0;
}``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````// Include namespace system
using System;
/*
C# Program
Calculate the Hamming distance between two strings
*/
public class Calculation
{
// Calculate hamming distance
public void hammingDistance(String str1, String str2)
{
// Get the length of string element
int n1 = str1.Length;
int n2 = str2.Length;
if (n1 != n2)
{
// When given string size are not equals
Console.Write(" String (" + str1 + "," + str2 + ") are not equals length\n");
}
else
{
// Used to store result
int result = 0;
// Loop controlling variable
int i = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (str1[i] != str2[i])
{
// When element of (i) location are not same
result++;
}
i++;
}
// Display calculated result
Console.Write("Hamming Distance of String (" + str1 + "," + str2 + ") is : " + result + "\n");
}
}
public static void Main(String[] args)
{
Calculation obj = new Calculation();
String str1 = "100101";
String str2 = "111001";
String str3 = "pineapple";
String str4 = "pinxxcode";
String str5 = "abc";
String str6 = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
}
}``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````<?php
/*
Php Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
// Calculate hamming distance
public	function hammingDistance(\$str1, \$str2)
{
// Get the length of string element
\$n1 = strlen(\$str1);
\$n2 = strlen(\$str2);
if (\$n1 != \$n2)
{
// When given string size are not equals
echo " String (". \$str1 .",". \$str2 .") are not equals length\n";
}
else
{
// Used to store result
\$result = 0;
// Loop controlling variable
\$i = 0;
// Compare string element of str1 and str2
while (\$i < \$n1)
{
if (\$str1[\$i] != \$str2[\$i])
{
// When element of (i) location are not same
\$result++;
}
\$i++;
}
// Display calculated result
echo "Hamming Distance of String (". \$str1 .",". \$str2 .") is : ". \$result ."\n";
}
}
}

function main()
{
\$obj = new Calculation();
\$str1 = "100101";
\$str2 = "111001";
\$str3 = "pineapple";
\$str4 = "pinxxcode";
\$str5 = "abc";
\$str6 = "abc";
// Test Cases
\$obj->hammingDistance(\$str1, \$str2);
\$obj->hammingDistance(\$str3, \$str4);
\$obj->hammingDistance(\$str5, \$str6);
}
main();``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````/*
Node Js Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
// Calculate hamming distance
hammingDistance(str1, str2)
{
// Get the length of string element
var n1 = str1.length;
var n2 = str2.length;
if (n1 != n2)
{
// When given string size are not equals
process.stdout.write(" String (" + str1 + "," + str2 + ") are not equals length\n");
}
else
{
// Used to store result
var result = 0;
// Loop controlling variable
var i = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (str1[i] != str2[i])
{
// When element of (i) location are not same
result++;
}
i++;
}
// Display calculated result
process.stdout.write("Hamming Distance of String (" + str1 + "," + str2 + ") is : " + result + "\n");
}
}
}

function main()
{
var obj = new Calculation();
var str1 = "100101";
var str2 = "111001";
var str3 = "pineapple";
var str4 = "pinxxcode";
var str5 = "abc";
var str6 = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
}
main();``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````#   Python 3 Program
#   Calculate the Hamming distance between two strings

class Calculation :
#  Calculate hamming distance
def hammingDistance(self, str1, str2) :
#  Get the length of string element
n1 = len(str1)
n2 = len(str2)
if (n1 != n2) :
#  When given string size are not equals
print(" String (", str1 ,",", str2 ,") are not equals length")
else :
#  Used to store result
result = 0
#  Loop controlling variable
i = 0
#  Compare string element of str1 and str2
while (i < n1) :
if (str1[i] != str2[i]) :
#  When element of (i) location are not same
result += 1

i += 1

#  Display calculated result
print("Hamming Distance of String (", str1 ,",", str2 ,") is : ", result )

def main() :
obj = Calculation()
str1 = "100101"
str2 = "111001"
str3 = "pineapple"
str4 = "pinxxcode"
str5 = "abc"
str6 = "abc"
#  Test Cases
obj.hammingDistance(str1, str2)
obj.hammingDistance(str3, str4)
obj.hammingDistance(str5, str6)

if __name__ == "__main__": main()``````

#### Output

``````Hamming Distance of String ( 100101 , 111001 ) is :  3
Hamming Distance of String ( pineapple , pinxxcode ) is :  5
Hamming Distance of String ( abc , abc ) is :  0``````
``````#   Ruby Program
#   Calculate the Hamming distance between two strings

class Calculation
#  Calculate hamming distance
def hammingDistance(str1, str2)
#  Get the length of string element
n1 = str1.length()
n2 = str2.length()
if (n1 != n2)
#  When given string size are not equals
print(" String (", str1 ,",", str2 ,") are not equals length\n")
else
#  Used to store result
result = 0
#  Loop controlling variable
i = 0
#  Compare string element of str1 and str2
while (i < n1)
if (str1[i] != str2[i])
#  When element of (i) location are not same
result += 1
end

i += 1
end

#  Display calculated result
print("Hamming Distance of String (", str1 ,",", str2 ,") is : ", result ,"\n")
end

end

end

def main()
obj = Calculation.new()
str1 = "100101"
str2 = "111001"
str3 = "pineapple"
str4 = "pinxxcode"
str5 = "abc"
str6 = "abc"
#  Test Cases
obj.hammingDistance(str1, str2)
obj.hammingDistance(str3, str4)
obj.hammingDistance(str5, str6)
end

main()``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0
``````
``````/*
Scala Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
// Calculate hamming distance
def hammingDistance(str1: String, str2: String): Unit = {
// Get the length of string element
var n1: Int = str1.length();
var n2: Int = str2.length();
if (n1 != n2)
{
// When given string size are not equals
print(" String (" + str1 + "," + str2 + ") are not equals length\n");
}
else
{
// Used to store result
var result: Int = 0;
// Loop controlling variable
var i: Int = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (str1(i) != str2(i))
{
// When element of (i) location are not same
result += 1;
}
i += 1;
}
// Display calculated result
print("Hamming Distance of String (" + str1 + "," + str2 + ") is : " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Calculation = new Calculation();
var str1: String = "100101";
var str2: String = "111001";
var str3: String = "pineapple";
var str4: String = "pinxxcode";
var str5: String = "abc";
var str6: String = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
}
}``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````
``````/*
Swift 4 Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
// Calculate hamming distance
func hammingDistance(_ str1: String, _ str2: String)
{
// Get the length of string element
let n1: Int = str1.count;
let n2: Int = str2.count;
if (n1 != n2)
{
// When given string size are not equals
print(" String (", str1 ,",", str2 ,") are not equals length");
}
else
{
let s1 = Array(str1);
let s2 = Array(str2);
// Used to store result
var result: Int = 0;
// Loop controlling variable
var i: Int = 0;
// Compare string element of str1 and str2
while (i < n1)
{
if (s1[i] != s2[i])
{
// When element of (i) location are not same
result += 1;
}
i += 1;
}
// Display calculated result
print("Hamming Distance of String (", str1 ,",", str2 ,") is : ", result );
}
}
}
func main()
{
let obj: Calculation = Calculation();
let str1: String = "100101";
let str2: String = "111001";
let str3: String = "pineapple";
let str4: String = "pinxxcode";
let str5: String = "abc";
let str6: String = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
}
main();``````

#### Output

``````Hamming Distance of String ( 100101 , 111001 ) is :  3
Hamming Distance of String ( pineapple , pinxxcode ) is :  5
Hamming Distance of String ( abc , abc ) is :  0``````
``````/*
Kotlin Program
Calculate the Hamming distance between two strings
*/
class Calculation
{
// Calculate hamming distance
fun hammingDistance(str1: String, str2: String): Unit
{
// Get the length of string element
var n1: Int = str1.count();
var n2: Int = str2.count();
if (n1 != n2)
{
// When given string size are not equals
print(" String (" + str1 + "," + str2 + ") are not equals length\n");
}
else
{
// Used to store result
var result: Int = 0;
// Loop controlling variable
var i: Int = 0;
// Compare string element of str1 and str2
while (i<n1)
{
if (str1[i] != str2[i])
{
// When element of (i) location are not same
result += 1;
}
i += 1;
}
// Display calculated result
print("Hamming Distance of String (" + str1 + "," + str2 + ") is : " + result + "\n");
}
}
}
fun main(args: Array<String>): Unit
{
var obj: Calculation = Calculation();
var str1: String = "100101";
var str2: String = "111001";
var str3: String = "pineapple";
var str4: String = "pinxxcode";
var str5: String = "abc";
var str6: String = "abc";
// Test Cases
obj.hammingDistance(str1, str2);
obj.hammingDistance(str3, str4);
obj.hammingDistance(str5, str6);
}``````

#### Output

``````Hamming Distance of String (100101,111001) is : 3
Hamming Distance of String (pineapple,pinxxcode) is : 5
Hamming Distance of String (abc,abc) is : 0``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.