2

I have an array of strings as I have shown below. I am iterating through the string array and printing the fourth char of each string element which is always 1.

What I've been trying to do is implement an if statement, so that if every fourth char of each element is 1 then to print something, but I am unsure how to access each of the elements that I am iterating through.

I have added an if statement with some pseudo code to try and highlight what I am trying to do. Thanks for your help.

       namespace ConsoleApplication6
       {
          class Program
              {
           static void Main()
              {
        string[] bitstrings = new string[16];
        bitstrings[0] = "00101";
        bitstrings[1] = "00001";
        bitstrings[2] = "00001";
        bitstrings[3] = "00101";
        bitstrings[4] = "00101";
        bitstrings[5] = "00101";
        bitstrings[6] = "00101";
        bitstrings[7] = "00101";
        bitstrings[8] = "00101";
        bitstrings[9] = "00101";
        bitstrings[10] = "00101";
        bitstrings[11] = "00101";
        bitstrings[12] = "00101";
        bitstrings[13] = "00101";
        bitstrings[14] = "00101";
        bitstrings[15] = "00101";

        for (int x = 0; x < bitstrings.Length; x++)
        {

            int s = bitstrings[x][4];
            Console.ReadLine();



        }

        if(/*all bitstrings[x][4] = 1*/)
        {
            //print something
        }

    }
}
 }

3 Answers 3

5

Your code is about there, you just need to remember that each character in the string, regardless of its value, is still a character. Your if statement also needed to be in the for loop.

for (int x = 0; x < bitstrings.Length; x++)
{
    //3 instead of 4 because arrays are 0 indexed
    if(bitstrings[x][3] == '1')
    {
       //valid char
    }
}
//Also don't need to read like every iteration.
Console.ReadLine(); 

Edit: if All 4th elements must be 1 then modify this to include a boolean, and break out if invalid char found

bool allOne = true;
for (int x = 0; x < bitstrings.Length; x++)
{
    //3 instead of 4 because arrays are 0 indexed
    if(bitstrings[x][3] != '1')
    {
       allOne = false;
       break;
    }
}
if(allOne)
{ 
   //Do something
}

Console.ReadLine(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Note: Purposely not created an answer with linq
Unquestionably the correct non-LINQ solution. Just a side-note (typo, really) there is no such thing as an if "loop" ;)
3
if (bitstrings.All(s => s[3] == '1'))
{
   //printSomething
}

This takes advantage of the LINQ "All" function which returns true if and only if all elements of the collections satisfy the provided predicate. The predicate, in this case, returns true if the fourth element of the passed string is the character '1'.

The fourth element is indexed with "3" because C# (like most languages) uses 0-based indices for their arrays. You also may need to add using System.Linq to your code file to use this solution.

Comments

0

Something like this?

static bool AllBitsEqual( IEnumerable<string> source , int offset , char value )
{
  return source.All( x => offset < x.Length && x[offset] == value ) ;
}

or

static bool AllBitsEqual( IEnumerable<string> source , int offset , char value )
{
  return source.Aggregate( true , (acc,s) => acc &= (s.Length > offset && s[offset] == value ) ) ;
}

or even...

static bool AllBitsEqual( IEnumerable<string> source , int offset , char value )
{
  bool flag = true ;
  foreach ( string s in source )
  {
    flag &= (s.Length > offset && s[offset] == value ) ;
  }
  return flag;
}

Comments

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.