Hi, this something very basic I know it, but how can I skip the spaces in this basic program please.
string Palabras = string.Empty;
//int ContEspacios;
Console.WriteLine("Escribe una palabra:");
Palabras = Console.ReadLine();
foreach (var X in Palabras.Length.ToString())
{
Console.WriteLine("Cuantas palabras tiene {0}:",X);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Loading


VulpesRank #55 · 29.2K reputationPosted Apr 10, 2013, 6:49 AM EDT
Try the following:
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Apr 10, 2013, 7:00 AM EDT
//int ContEspacios;
Console.WriteLine("Escribe una palabra:");
Palabras = Console.ReadLine();
int ContEspacios = 0;
foreach (char s in Palabras)
{
if (s == 32)
{
ContEspacios++;
}
}
Console.WriteLine("Total Number of words : {0}", ++ContEspacios);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Freddy Abreu40 reputationPosted Apr 10, 2013, 7:14 AM EDT
This is the best forum I've ever been before.
Freddy Abreu40 reputationPosted Apr 10, 2013, 7:20 AM EDT
you mean 32 bits? right?
Forgive me for the ignorant question.
VulpesRank #55 · 29.2K reputationPosted Apr 10, 2013, 7:35 AM EDT
if (s == 32)
then the char 's' is converted to its Unicode equvalent before making the comparison.
The reason why Sandeep's code is shorter than mine is because mine deals with the possibility that there could be multiple consecutive spaces between words.
Freddy Abreu40 reputationPosted Apr 10, 2013, 8:05 AM EDT
Blessings.