Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 23 May 2017

String based program


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.
 
string based program by imagination hunt
String based program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

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

Previous - String based program #12

Next – String based program #14

Click imagination hunt to read latest blogs.


Keep learning and sharing...

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...