Various problems
Gets and the input buffer
The keyboard input makes use of a buffer which is known as stdin the scanf function takes the values of that buffer it needs and the rest leaves it there. This can be useful in certain cases, and always have to be taken into account when reading keyboard data.
#include<stdio.h>
#include<stdlib.h>
int main(){
char *pe;
int tam;
int i, aux=0;
printf("Cuantos elementos tendra el vector:");
scanf("%d",&tam);
pe=malloc(tam*sizeof(char));
When you read the variable tam you only take the decimal value tam, the rest of the input stays in the input buffer and when you make the call gets
what it receives is the line break. Therefore, you do not read the chain and go directly to process it.
To avoid this error, a buffer cleaner is placed, the fflush function indicating which buffer you want to clean, in this case stdin
fflush(stdin);
gets(pe);
Strings and Chars
strings and character strings; the second error that you will have once you read the string is when you print the string, what you seem to want to do is iterate over each item and print it, but at the time of printing you tell the printf function that prints a %s
which means a string but passes it as an argument pe[i]
which in this case is a character. therefore tell the printf function to print a character with% c.
printf("Contenido del vector dinamico:");
for(i=0;i<tam;i++)
{
printf("%c ",pe[i]);
}
Iteracion before calling i
for(i=0;i<tam;i++)
{
If you want to make use of the iteration of the variable i you must place the for cycle before, otherwise you would be taking the value that was stored in the last cycle for which is tam + 1 and which is a non-accessible position of your vector pe
which inevitably leads to an error.
Correct use of the conditional
What you want is "If PE [i] is not vocal, increase the aux counter".
But what you have is very different if(pe[i]!='a'||'e'||'i'||'o'||'u')
can be "translated" as:
if (eg [i] is not a) Or e is true Or i is true Or is true ...
Each of these variables will be true because they are not NULL or 0, and therefore that loop will always be true. you must make each condition a comparison
if(pe[i]!='a'&& pe[i]!='e' && pe[i]!='i'&& pe[i]!='o'&& pe[i]!='u')
{
aux++;
}
}
Printf and variable types
Finally the variable aux is of type int and to print it you must use the type% d in the printf.
printf("el numero de consonates de la frase es de: %d" ,aux);
free(pe);
return 0;
}
This way it worked for me. I hope it helps.
PS:
I prefer the use of fgets over the use of gets
Regarding the use of% in printf you can look at this other question
about the specifiers of format in printf
IMPORTANT
Thanks to a comment from @NaCl I understood that being the buffer stdin an input buffer and that the fflush function is designed to clean output buffers, its behavior is undefined, therefore especially in Linux environments using fflush (stdin) , you should avoid doing
fflush(stdin);
gets(pe);
Instead, I recommend using
scanf(" %s",pe);
// ↑ colocando un espacio en blanco antes del especificador
// scanf ignorará saltos de linea y espacios en blanco