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.
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();
}
}
}