Although the answer of Thiago Loureiro solves much of the problem, because it is an exercise, I thought it valid to leave an answer explaining some important points that will clarify your doubts, because it seems to me that the exercise is, in fact, directed to work with vectors and an input and output flow of your program.
There are several errors in its logic and implementation. The vector always has 5 positions with value \0
because at the beginning of your code you declare it that way,
char[] nome_completo = new char[5];
and never popula, the result is an array of char with 5 empty positions.
The method WriteLine()
is for you to write things on the console, and not to read information the command below does not assign the input value to your variable:
Console.WriteLine("Digite o seu nome completo: ", nome_completo);
Your application would be in case you already have a stated initial value for nome_completo
, but still wouldn’t make much sense, follow an example of a more usual application with this Overload and a placeholder;
char caractere = 'a'; //poderia ser uma string
Console.WriteLine("Digite o seu nome completo: (iremos contar a quantidade do caractere \"{0}\")", caractere);
See that your Console.ReadLine();
does not assign any value to it and even if it did the input could not have more than 5 characters, because it would exceed the pre-set limit for its vector and still find an error because the method ReadLine()
actually returns a type string
and not a char[]
, the following conversion is required:
nome_completo = Console.ReadLine().ToCharArray();
But still you would find an error, however much you assign a higher initial value to your vector, say new char[1024]
and assign its value through ReadLine().TocharArray()
, its no for declaration will result in a new error, since now that a value has been assigned to its vector it no longer has empty positions and its condition nome_completo[conta] != '\0'
will never be false, resulting in index error why conta
will continue to be increased beyond the size of nome_completo
Then comes your comparison to find the vowel "a", you are testing the character 'a' which is different from 'A' or 'á' or 'ã' and so on. Also, within the same block you are typing the result as total (which is actually partial) and making the program wait on account of the ReadLine()
.
Nevertheless, its WriteLine()
has a syntax error that does not actually state the value assigned to the counter vogal
. In such a case, the same technique would be used at the beginning of the reply.
//O problema está na "," em '"Total de ",'
//Console.WriteLine("Total de ", vogal + "letras");
Console.WriteLine("Total de {0} letras", vogal);
Below is a review of the code, applying the points observed previously, but I recommend you to go making the changes as presented in the answer so you can better understand the causes and consequences:
static void Main(string[] args)
{
int vogal = 0;
char[] caracteres = { 'a', 'á', 'â', 'ã', 'ä', 'à' };
Console.WriteLine("Digite o seu nome completo:");
char[] nome_completo = Console.ReadLine().ToLower().ToCharArray();
for (int conta = 0; conta < nome_completo.Length; conta++)
{
if (caracteres.Contains(nome_completo[conta]))
{
vogal++;
}
}
Console.WriteLine("Total de {0} letras \"{1}\".", vogal, caracteres[0]);
Console.Read();
Console.ReadKey();
}
was coming to add a vowel list too ;) +1
– Barbetta
good master, thanks for adding more content! Always good to increment! :)
– Thiago Loureiro
Thanks for the comments Leandro Angelo, I learned a lot from the concepts.
– Leandro Azevedo