String program #13 (Check
all characters in string 2 is contained in string1)
Enter string1: imagination
Enter string2: sarcastical
O/p: -
String2 doesnot contain all characters
of string1
The program for the string is written
in C# programming language and will accept two strings as input. The logic is
to check all characters of string1 is contained in string2.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
using System.Text;
namespace patternProblem.String
{
class String13 //check all characters in string 2 is contained in string1
{
public static void Main()
{
Console.Write("Enter string1: ");
string str1 = Console.ReadLine();
Console.Write("Enter string2: ");
string str2 = Console.ReadLine();
int count = 0;
StringBuilder strCharMatched = new StringBuilder();
for (int i =
str2.Length - 1; i >= 0; i--)
{
for (int j = 0; j
<= str1.Length - 1; j++)
{
if (str2[i] == str1[j])
{
strCharMatched.Append(str2[i]);
strCharMatched.Append(",");
count++;
break;
}
}
}
if (str2.Length == count)
{
Console.WriteLine("String2 contain all
characters in string1");
Console.WriteLine("All characters that are
matched: {0}", strCharMatched);
}
else
{
Console.WriteLine("String2 doesnot contain all
characters in string1");
}
}
}
}
Output:
Based on
the input, program will check whether string2 contain all characters in string1.
The output is shown below:
Enter string1: imagination
Enter string2: namingition
String2 contain all characters of string1
Enter string1: imagination
Enter string2: sarcastical
String2 doesnot contain all characters of string1
Enter string1: liveviar
Enter string2: rivale
String2 doesnot contain all characters of string1
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Keep learning and sharing...
No comments:
Post a Comment