🔒 Closed Patulong po sa mga expert sa programming Thankyou <3

Status
Not open for further replies.

heyz

Leecher
Write a c# program that creates an array containing all letters from the alphabet (A-Z). Read a word from the console and print the index of each of its letters in the array.
 
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace array_alphabet
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] alphabet= new char[26] ;
            string Input;
            string IndexOfChar="";

            int index = 0;
            for (char c = 'A';c<='Z';c++)
            {
                alphabet[index] = c;
                index++;
            }

            Console.WriteLine("Enter a word:");
            Input = Console.ReadLine();

            Input = Input.ToUpper();
            
            foreach(char c in Input)
            {
                for (int i=0;i<26;i++)
                {
                     if (alphabet[i]==c)
                    {
                        IndexOfChar = IndexOfChar + i + " ";
                        break;
                    }
                }
            }
            Console.WriteLine(IndexOfChar);

            Console.ReadKey();
        }
    }
}

Kaso mabagal to 2 for loops.

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace array_alphabet
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] alphabet= new char[26] ;
            string Input;
            string IndexOfChar="";

            int index = 0;
            for (char c = 'A';c<='Z';c++)
            {
                alphabet[index] = c;
                index++;
            }

            Console.WriteLine("Enter a word:");
            Input = Console.ReadLine();

            Input = Input.ToUpper();
            
            foreach(char c in Input)
            {
                IndexOfChar = IndexOfChar + (Array.IndexOf(alphabet, c)) + " ";
            }
            Console.WriteLine(IndexOfChar);

            Console.ReadKey();
        }
    }
}

baka ito yung hinahanap

Array.IndexOf(yung alphabet array, yung hinahanap na character)

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace array_alphabet
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] alphabet= new char[26] ;
            string Input;

            int index = 0;
            for (char c = 'A';c<='Z';c++)
            {
                alphabet[index] = c;
                index++;
            }

            Console.WriteLine("Enter a word:");
            Input = Console.ReadLine().ToUpper();
            
            foreach(char c in Input)
            {
                Console.Write ((Array.IndexOf(alphabet, c)) + " ");
            }

            Console.ReadKey();
        }
    }
}

Inalis ko yung unnecessary variable/s.
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 509
    Views
  • 3
    Participants
Last reply from:
Aoshinomori

Online now

Members online
628
Guests online
653
Total visitors
1,281

Forum statistics

Threads
2,276,935
Posts
28,973,174
Members
1,230,625
Latest member
heyuhe
Back
Top