Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

How can I use foreach statement?

I wrote a program that reads 10 characters into an array and prints out the array sorted in alphabetic order.However, I want to use foreach statement in my program.How do I that?

Here is my code:
     static void Main(string[] args)
          {
               char []c = new char[10];
               for(int i = 0; i<c.Length ; i++)
               {
                Console.WriteLine("Enter a letter: ");
                    c[i]=Char.Parse(Console.ReadLine());

               }
               for(int i = 0;i < c.Length; i++)
                for(int j = 0; j < c.Length-1; j++)
                   {
                         if(c[i]>c[j])
                         {
                              char letter       = c[i];
                              c[i]              = c[j];
                              c[j]              = letter;
                         }
                }
             for(int i = 0;i < c.Length; i++)
                    Console.Write(c[i]+ " ");
         
          }
     }
0
tonino_31
Asked:
tonino_31
  • 3
  • 3
1 Solution
 
tonino_31Author Commented:
thank you for your time
0
 
Arthur_WoodCommented:
where do you want to use foreach?


for example, you can do this:

           foreach(char letter in c)
                   Console.Write(letter + " ");
 
is that what you had in mind?

AW
0
 
tonino_31Author Commented:
thanks Arthur. Infact, I tried the code in the last for statement in my program and it compiles but instead of sorting the letters from the first letter is goes backward. for example if the letters were {s,e,u,j,z,k} it gives me {z,u,s,k,j,e}

this is what I did to my code:
static void Main(string[] args)
{
   char []c = new char[10];
   for(int i = 0; i<c.Length ; i++)
    {
      Console.WriteLine("Enter 10 letters: ");
        c[i]=Char.Parse(Console.ReadLine());
    }
    //for(int i = 0;i < c.Length; i++)
     //for(int j = 0; j < c.Length-1; j++)
     {
       if(c[i]>c[j])
       {
          char letter       = c[i];
          c[i]              = c[j];
          c[j]              = letter;
      }
        }
     
      foreach(char letter in c)
          Console.Write(letter + "");
}

I want to use the foreach statements where the comments are
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Arthur_WoodCommented:
you can't use foreach there, as you would be changing the letter itself (the value in the foreach loop).

As the For loops are currently coded, you are not changing the value of the loop index, but rather interchanging the values of the array that the loop index points to.

change it like this:

for(int i = 0;i < c.Length; i++)
    for(int j = 0; j < c.Length-1; j++)
    {
      if(c[i]<c[j])
      {
         char letter       = c[i];
         c[i]              = c[j];
         c[j]              = letter;
     }
       }


that should reverse the sort.

AW
0
 
tonino_31Author Commented:
It worked thanks for your time.
0
 
Arthur_WoodCommented:
you're welcome..

AW
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now