String program #3
(Find
at which position strings are not equal)
Case1:
Enter String1: liveviar
Enter String2: livevlar
Strings are not equal.
String1 doesnot match at index: 6,
i.e., i
String2 doesnot match at index: 6,
i.e., l
Case2:
Enter String1: liveviar
Enter String2: imaginationhunt
Strings are not equal they are of
different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of
different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of
different length.
String1 length = 8
String2 length = 15
Case3:
Enter String1: liveviar
Enter String2: liveviar
Strings are equal.
Strings are equal.
Strings are equal.
The program for the string program is
written in C# programming language and will accept a two string as input. The
loops will iterate based on the entry of string and compare the first string
with the second string and return the output.
String based program |
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
using System.Text;
namespace patternProblem.String
{
class String3 //find at which position strings are not equal
{
static void Main(string[] args)
{
Console.Write("Enter String1: ");
string str1 = Console.ReadLine();
Console.Write("Enter String2: ");
string str2 = Console.ReadLine();
//Method-1 (Using in-built CompareTo() function)
if
(str1.CompareTo(str2) == 0)
{
Console.WriteLine("Strings are equal.");
}
else
{
notEqualWithVoidReturnType(str1, str2);
}
//Method-2 (Using if-else condition)
if
(str1 == str2)
{
Console.WriteLine("Strings are equal.");
}
else
{
notEqualWithVoidReturnType(str1, str2);
}
//Method-3 (Using ternary operator)
string result = (str1 == str2) ? "Strings are equal." :
notEqualWithStringReturnType(str1, str2);
Console.WriteLine(result);
Console.ReadKey();
}
private static string
notEqualWithStringReturnType(string
string1, string string2)
{
StringBuilder strbuild = new
StringBuilder();
string str1 = string1;
string str2 = string2;
char[]
strToChar1 = str1.ToCharArray();
char[]
strToChar2 = str2.ToCharArray();
if
(str1.Length != str2.Length)
{
strbuild.Append("Strings are not equal
they are of different length.");
strbuild.Append("\n");
strbuild.Append("String1 length =
");
strbuild.Append(str1.Length);
strbuild.AppendLine();
strbuild.Append("String2 length =
");
strbuild.Append(str2.Length);
return strbuild.ToString();
}
else
{
for (int i = 0, j = 0; strToChar1[i]
> str1.Length; i++, j++)
{
if (strToChar1[i] !=
strToChar2[j])
{
strbuild.Append("Strings are not
equal.");
strbuild.AppendLine();
strbuild.Append("String1 doesnot match
at index: ");
strbuild.Append(i + 1);
strbuild.Append(", i.e., ");
strbuild.Append(strToChar1[i]);
strbuild.AppendLine();
strbuild.Append("String2 doesnot match
at index: ");
strbuild.Append(j + 1);
strbuild.Append(", i.e., ");
strbuild.Append(strToChar2[j]);
return strbuild.ToString();
}
}
}
return "";
}
private static void notEqualWithVoidReturnType(string string1, string string2)
{
string str1 = string1;
string str2 = string2;
char[]
strToChar1 = str1.ToCharArray();
char[]
strToChar2 = str2.ToCharArray();
if
(str1.Length != str2.Length)
{
Console.WriteLine("Strings are not equal they are of
different length.");
Console.WriteLine("String1 length = {0}\nString2
length = {1}",
str1.Length, str2.Length);
}
else
{
for (int i = 0, j = 0; strToChar1[i]
> str1.Length; i++, j++)
{
if (strToChar1[i] !=
strToChar2[j])
{
Console.WriteLine("Strings are not
equal.");
Console.WriteLine("String1 doesnot match at index: {0}, i.e.,
{1}\nString2 doesnot match at index: {2}, i.e., {3}", i + 1, strToChar1[i], j +
1, strToChar2[j]);
break;
}
}
}
}
}
}
Output:
Case1:
The input
string1 here is “liveviar” and string2 is “livevlar”. So, based on the string
comparison it will return at which position strings are not equal. The output
is shown below:
Case1:
Enter String1: liveviar
Enter String2: livevlar
Strings are not equal.
String1 doesnot match at index: 6, i.e., i
String2 doesnot match at index: 6, i.e., l
Press any key to continue . . .
Case2:
The input
string1 here is “liveviar” and string2 is “imaginationhunt”. So, based on the
string comparison it will firstly check whether strings are of same length. If
yes, only then it’ll return at which position strings are not equal. The output
is shown below:
Case2:
Enter String1: liveviar
Enter String2: imaginationhunt
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Strings are not equal they are of different length.
String1 length = 8
String2 length = 15
Press any key to continue . . .
Case3:
The input
string1 here is “liveviar” and string2 is “liveviar”. So, based on the string
comparison it will return whether the strings are equal or not. The output is
shown below:
Case3:
Enter String1: liveviar
Enter String2: liveviar
Strings are equal.
Strings are equal.
Strings are equal.
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Related Question:
-
New to program try First C#
Program
Q-1. Can we use placeholder in StringBuilder? How?
string str1=”liveviar”;
StringBuilder strbuild = new StringBuilder();
strbuild.Append("String1 length = {0}",str1.Length);
Ans. Yes. But it’s not good to use placeholder in StringBuilder.
Using StringBuilder will replace placeholder {}. See the below practical implementation
of the above code to understand better.
Practical
Implementation:
strbuild.Append("String1
length = ");
strbuild.Append(str1.Length);
Q-2. What will be the output for the code?
int j=2;
strbuild.Append("String1 doesnot match at index: " + j + 1);
A) String1 doesnot match at index: 21
B) String1 doesnot match at index: 3
C) Error on '+' operator: Concatentation is not allowed in string
builder
Ans. Option (A) is correct.
String1
doesnot match at index: 21
StringBuilder
adds a variable to a hardcode number.
Keep learning and sharing...
No comments:
Post a Comment