Skip to content

Instantly share code, notes, and snippets.

@Reflej0
Created April 6, 2017 21:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Reflej0/a242df9072534417c2cd3e562b785867 to your computer and use it in GitHub Desktop.
Save Reflej0/a242df9072534417c2cd3e562b785867 to your computer and use it in GitHub Desktop.
Algoritmo en C# para contar las palabras y caracteres dentro de una frase.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Frase
{
public static int[] PalabrasCaracteres(string frase){
int palabras = 0;
int caracterestotales = 0;
for (int i = 0; i < frase.Length; i++)
{
if (frase[i] == ' ' || frase[i] == '.')
{
palabras++;
}
}
int[] PalabrasCaracteres = new int[2];
caracterestotales = frase.Length - palabras;
PalabrasCaracteres[0] = palabras;
PalabrasCaracteres[1] = caracterestotales ;
return PalabrasCaracteres;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Ingrese una frase");
string frase = Console.ReadLine();
int[] resultado = Frase.PalabrasCaracteres(frase);
Console.WriteLine("La frase tiene {0} palabras", resultado[0]);
Console.WriteLine("La frase tiene {0} caracteres", resultado[1]);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment