Skip to content

Instantly share code, notes, and snippets.

@MrDave1999
Last active August 9, 2019 19:48
Show Gist options
  • Save MrDave1999/db0eadcdc87af26cb495b869e1a98c89 to your computer and use it in GitHub Desktop.
Save MrDave1999/db0eadcdc87af26cb495b869e1a98c89 to your computer and use it in GitHub Desktop.
Programa que detecta la cantidad de veces que una letra, pueda aparecer en una oración.
using System;
class Alphabet
{
private char[]abc = new char[26];
private int[] countUpper = new int[26];
private int[] countLower = new int[26];
public Alphabet()
{
//Para rellenar el abecedario en mayuscula en "char[] abc"
for(int i = 0; i != 26; ++i)
abc[i] = (char)(i + 'A');
}
public void DetectUpperOrLower(char charx)
{
int pos;
bool convertion = false;
if(charx >= 'a' && charx <= 'z')
{
convertion = true;
charx -= (char)32;
}
//La clase SearchBinary está en: https://gist.github.com/MrDave1999/843654eb567b730bd9af8755ec1a6fa0
pos = SearchBinary.BBIterative<char>(abc, charx);
if(convertion)
++countLower[pos];
else
++countUpper[pos];
}
public void ShowChars()
{
for(int i = 0; i != 26; ++i)
{
if(countUpper[i] != 0)
Console.WriteLine("-> {0} : {1}\n", abc[i], countUpper[i]);
else if(countLower[i] != 0)
Console.WriteLine("-> {0} : {1}\n", (char)(abc[i] + 32), countLower[i]);
}
for(int i = 0; i != 26; ++i)
{
countUpper[i] = 0;
countLower[i] = 0;
}
}
}
using System;
class Program
{
static void Main()
{
string sentence;
Alphabet abc = new Alphabet();
while(true)
{
Console.Write("Ingrese una oracion:\n");
sentence = Console.ReadLine();
for(int i = 0, len = sentence.Length; i != len; ++i)
{
if((sentence[i] >= 'a' && sentence[i] <= 'z') || (sentence[i] >= 'A' && sentence[i] <= 'Z'))
abc.DetectUpperOrLower(sentence[i]);
}
abc.ShowChars();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment