1

I want the output like this :

A=1
C=3
D=9
e=5

If second array value is 0 I don't want that value how to achieve this output in c#?

private static void Main(string[] args)
{
    string[] a = new string[] { "a", "b", "c", "d" ,"e"};
    string[] b = new string[] {1,0,3,0,5 };

    foreach (string tmp in a)
    {
        bool existsInB = false;
        foreach (string tmp2 in b)
        {
             if (tmp == tmp2)
             {
                 existsInB = true;
                 break;
             }
         }

         if (!existsInB)
         {
             Console.WriteLine(string.Format("{0} is not in b", tmp));
         }
     }

     Console.ReadLine();
}

In that two arrays I want to print values like a=1,c=3,e=5, I don't want to print second array zero value. How do I achieve this?

I need a output in c#: a=1 c=3 e=5

2 Answers 2

3

I believe this will work for you

class Program
{
    static void Main(string[] args)
    {
        string[] a = new string[] { "a", "b", "c", "d", "e" };
        int[] b = new int[] { 1, 0, 3, 0, 5 };

        for (int i = 0; i < a.Length; i++)
        {
            if (b[i] != 0)
                Console.WriteLine(a[i] + "=" + b[i]);
        }

        Console.ReadLine();
    }
}

Dictionary version (as per Matt Murdock's suggestion):

        Dictionary<string, int> ab = new Dictionary<string, int>
        {
            {"a", 1},
            {"b", 0},
            {"c", 3},
            {"d", 0},
            {"e", 5}
        };

        foreach(var pair in ab)
        {
            if(pair.Value != 0)
                Console.WriteLine(pair.Key + "=" + pair.Value);
        }
Sign up to request clarification or add additional context in comments.

5 Comments

To make it more dynamic, I think you can replace the loop count with no. of elements in the array.
You are right. But if it is me I will use a Dictionary. :)
Better choice, Yes. You can recommend it in your answer too. Its always good to represent optimal ways of solving a problem rather than taken approach and leave the choice to the OP.
it will print only a=1 , i want to print all values like, a=1,c=3,e=5
@kavitha I edited my code to include my entire code. Hope it will help you :)
0

The other answer by @interceptwind has already given a solution, this is just an alternative solution with Linq

string[] a = new string[] { "a", "b", "c", "d", "e" };
string[] b = new string[] { "1", "0", "3", "9", "5" };

var result = a.Zip(b, (strA, strB) => string.Format("{0}={1}", strA.ToUpper(), strB))
                .Where(s => !s.Contains("=0"))
                .ToArray();

12 Comments

i replace the loop count but i will get only a=1
please suggest me how to get a=1,c=3,d=9,e=5 using for loop i want to achieve this output.
I didn't understand what you mean by "i replace the loop count". Just copy paste this exact code and run, you'll get your desired result.
@KAvitha is interceptwind solution didn't work for you?
string[] a = new string[] { "a", "b", "c", "d", "e" }; int[] b = new int[] { 1, 0, 3, 0, 5 }; //Changed to int for (int i = 0; i < a.Length; i++) { if(b[i] != 0) Console.WriteLine(a[i] + "=" + b[i]); } just i copy paste this code the output is came A=1. But i want to print c=3,d=9,e=5
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.