I'm trying to pass a local array from a function letter_remover, which reads an original array, removes vowels + h, w and y, then copies that into a new array. This new array is then passed to main.
For example input plutonium would become pltnm. However when I call the function in main and print out the new array, it will duplicate some letters, for instance plltnm is printed.
void array_filler (char a[]);
char * letter_remover (char b[]);
int main (void)
{
char name[MAX];
char *p;
int i;
array_filler(name);
p = letter_remover(name);
printf("Local array passed back: ");
for (i = 0; i < MAX; i++)
{
printf("%s", p);
p++;
}
return 0;
}
If I print the new array created in the function letter_remover, it prints correctly. The letter_remover function creates the new array as a static char[] array and returns a char *
array_filler contains:
void array_filler (char a[])
{
printf("Type name: ");
int i = 0, c;
while ((c = getchar()) != '\n')
{
c = tolower(c);
if (isalpha(c))
{
a[i] = c;
i++;
}
}
a[i] = '\0';
printf("Name inside array: %s\n", a);
}
letter_remover contains:
char * letter_remover (char b[])
{
int i;
static char c[MAX];
char a[] = "aeiouywh";
printf("Name without forbidden characters: ");
for (i = 0; b[i] != '\0'; i++)
{
if (!strchr(a, b[i]))
{
c[i] = b[i];
printf("%c", c[i]);
}
}
c[i] = '\0';
printf("\n");
return c;
}
array_fillerandletter_remover?forloop in themainfunction seems strange.Why don't you printponly once?