Check if two strings are equal
Here given code implementation process.
//C Program
//Check if two strings are equal
#include <stdio.h>
//Function which is determine whether given string are identical or not
//Time complexity O(n)
void is_equals(char str_a[], char str_b[], int size_a, int size_b)
{
int status = 0;
//Compare two string size
if (size_a == size_b)
{
status = 1;
for (int i = 0; i < size_a && status == 1; ++i)
{
//Compare character of given str data
if (str_a[i] != str_b[i])
{
//When not equal to each other
status = 0;
}
}
}
//Display given string text
printf("\n Given A : [%s] And B : [%s]\n", str_a, str_b);
if (status == 1)
{
//When two strings are equal
printf(" Is Equals \n");
}
else
{
//When two strings are not equal
printf(" Is Not Equals \n");
}
}
int main(int argc, char
const *argv[])
{
//Define character of string data
char str1[] = "Write a code";
char str2[] = "write a code";
char str3[] = "Write a code";
char str4[] = "Write a code ";
int size_a = sizeof(str1) / sizeof(str1[0]) - 1;
int size_b = sizeof(str2) / sizeof(str2[0]) - 1;
//Test Case A
is_equals(str1, str2, size_a, size_b);
//Test Case B
size_b = sizeof(str3) / sizeof(str3[0]) - 1;
is_equals(str1, str3, size_a, size_b);
//Test Case C
size_b = sizeof(str4) / sizeof(str4[0]) - 1;
is_equals(str1, str4, size_a, size_b);
return 0;
}
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
//Include header file
#include <iostream>
using namespace std;
// C++ program
// Check if two strings are equal
class MyString
{
public:
//Function which is determine whether given string are identical or not
//Time complexity O(n)
void is_equals(string str_a, string str_b)
{
bool status = false;
//Compare two string size
if (str_a.size() == str_b.size())
{
status = true;
for (int i = 0; i < str_a.size() && status == true; ++i)
{
//Compare two character of given str data
if (str_a[i] != str_b[i])
{
//When not equal to each other
status = false;
}
}
}
cout << "\n Given A : [" << str_a << "] And B : [" << str_b << "]\n";
if (status == true)
{
//When two strings are equal
cout << " Is Equals \n";
}
else
{
//When two strings are not equal
cout << " Is Not Equals \n";
}
}
};
int main()
{
MyString obj = MyString();
string text = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
return 0;
}
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
// Java program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
public void is_equals(String str_a, String str_b)
{
boolean status = false;
//Compare two string size
if (str_a.length() == str_b.length())
{
status = true;
for (int i = 0; i < str_a.length() && status == true; ++i)
{
//Compare two character of given str data
if (str_a.charAt(i) != str_b.charAt(i))
{
//When not equal to each other
status = false;
}
}
}
System.out.print("\n Given A : [" + str_a + "] And B : [" + str_b + "]\n");
if (status == true)
{
//When two strings are equal
System.out.print(" Is Equals \n");
}
else
{
//When two strings are not equal
System.out.print(" Is Not Equals \n");
}
}
public static void main(String[] args)
{
MyString obj = new MyString();
String text = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
}
}
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
//Include namespace system
using System;
// C# program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
public void is_equals(String str_a, String str_b)
{
Boolean status = false;
//Compare two string size
if (str_a.Length == str_b.Length)
{
status = true;
for (int i = 0; i < str_a.Length && status == true; ++i)
{
//Compare two character of given str data
if (str_a[i] != str_b[i])
{
//When not equal to each other
status = false;
}
}
}
Console.Write("\n Given A : [" + str_a + "] And B : [" + str_b + "]\n");
if (status == true)
{
//When two strings are equal
Console.Write(" Is Equals \n");
}
else
{
//When two strings are not equal
Console.Write(" Is Not Equals \n");
}
}
public static void Main(String[] args)
{
MyString obj = new MyString();
String text = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
}
}
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
<?php
// Php program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
public function is_equals($str_a, $str_b)
{
$status = false;
//Compare two string size
if (strlen($str_a) == strlen($str_b))
{
$status = true;
for ($i = 0; $i < strlen($str_a) && $status == true; ++$i)
{
//Compare two character of given str data
if ($str_a[$i] != $str_b[$i])
{
//When not equal to each other
$status = false;
}
}
}
echo "\n Given A : [". $str_a ."] And B : [". $str_b ."]\n";
if ($status == true)
{
echo " Is Equals \n";
}
else
{
echo " Is Not Equals \n";
}
}
}
function main()
{
$obj = new MyString();
$text = "Write a code";
//Simple test
$obj->is_equals($text, "write a code");
$obj->is_equals($text, "Write a code");
$obj->is_equals($text, "Write a code ");
}
main();
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
// Node Js program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
is_equals(str_a, str_b)
{
var status = false;
//Compare two string size
if (str_a.length == str_b.length)
{
status = true;
for (var i = 0; i < str_a.length && status == true; ++i)
{
//Compare two character of given str data
if (str_a[i] != str_b[i])
{
//When not equal to each other
status = false;
}
}
}
process.stdout.write("\n Given A : [" + str_a + "] And B : [" + str_b + "]\n");
if (status == true)
{
process.stdout.write(" Is Equals \n");
}
else
{
process.stdout.write(" Is Not Equals \n");
}
}
}
function main()
{
var obj = new MyString();
var text = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
}
main();
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
# Python 3 program
# Check if two strings are equal
class MyString :
# Function which is determine whether given string are identical or not
# Time complexity O(n)
def is_equals(self, str_a, str_b) :
status = False
# Compare two string size
if (len(str_a) == len(str_b)) :
status = True
i = 0
while (i < len(str_a) and status == True) :
# Compare two character of given str data
if (str_a[i] != str_b[i]) :
# When not equal to each other
status = False
i += 1
print("\n Given A : [", str_a ,"] And B : [", str_b ,"]\n", end = "")
if (status == True) :
print(" Is Equals \n", end = "")
else :
print(" Is Not Equals \n", end = "")
def main() :
obj = MyString()
text = "Write a code"
# Simple test
obj.is_equals(text, "write a code")
obj.is_equals(text, "Write a code")
obj.is_equals(text, "Write a code ")
if __name__ == "__main__": main()
Output
Given A : [ Write a code ] And B : [ write a code ]
Is Not Equals
Given A : [ Write a code ] And B : [ Write a code ]
Is Equals
Given A : [ Write a code ] And B : [ Write a code ]
Is Not Equals
# Ruby program
# Check if two strings are equal
class MyString
# Function which is determine whether given string are identical or not
# Time complexity O(n)
def is_equals(str_a, str_b)
status = false
# Compare two string size
if (str_a.length() == str_b.length())
status = true
i = 0
while (i < str_a.length() && status == true)
# Compare two character of given str data
if (str_a[i] != str_b[i])
# When not equal to each other
status = false
end
i += 1
end
end
print("\n Given A : [", str_a ,"] And B : [", str_b ,"]\n")
if (status == true)
# When two strings are equal
print(" Is Equals \n")
else
# When two strings are not equal
print(" Is Not Equals \n")
end
end
end
def main()
obj = MyString.new()
text = "Write a code"
# Simple test
obj.is_equals(text, "write a code")
obj.is_equals(text, "Write a code")
obj.is_equals(text, "Write a code ")
end
main()
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
// Scala program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
def is_equals(str_a: String, str_b: String): Unit = {
var status: Boolean = false;
//Compare two string size
if (str_a.length() == str_b.length())
{
status = true;
var i: Int = 0;
while (i < str_a.length() && status == true)
{
//Compare two character of given str data
if (str_a(i) != str_b(i))
{
//When not equal to each other
status = false;
}
i += 1;
}
}
print("\n Given A : [" + str_a + "] And B : [" + str_b + "]\n");
if (status == true)
{
//When two strings are equal
print(" Is Equals \n");
}
else
{
//When two strings are not equal
print(" Is Not Equals \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
}
}
Output
Given A : [Write a code] And B : [write a code]
Is Not Equals
Given A : [Write a code] And B : [Write a code]
Is Equals
Given A : [Write a code] And B : [Write a code ]
Is Not Equals
// Swift program
// Check if two strings are equal
class MyString
{
//Function which is determine whether given string are identical or not
//Time complexity O(n)
func is_equals(_ text_a: String, _ text_b: String)
{
var status: Bool = false;
//Convert into array
var str_a = Array(text_a);
var str_b = Array(text_b);
//Compare two string size
if (str_a.count == str_b.count)
{
status = true;
var i: Int = 0;
while (i < str_a.count && status == true)
{
//Compare two character of given str data
if (str_a[i] != str_b[i])
{
//When not equal to each other
status = false;
}
i += 1;
}
}
print("\n Given A : [", text_a ,"] And B : [", text_b ,"]\n", terminator: "");
if (status == true)
{
print(" Is Equals \n", terminator: "");
}
else
{
print(" Is Not Equals \n", terminator: "");
}
}
}
func main()
{
let obj: MyString = MyString();
let text: String = "Write a code";
//Simple test
obj.is_equals(text, "write a code");
obj.is_equals(text, "Write a code");
obj.is_equals(text, "Write a code ");
}
main();
Output
Given A : [ Write a code ] And B : [ write a code ]
Is Not Equals
Given A : [ Write a code ] And B : [ Write a code ]
Is Equals
Given A : [ Write a code ] And B : [ Write a code ]
Is Not Equals
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.
New Comment