0

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;
}
10
  • What is "local arrray"? Commented Oct 16, 2015 at 13:30
  • What are the contents of array_filler and letter_remover? Commented Oct 16, 2015 at 13:30
  • @MikeCAT It is already a static array Commented Oct 16, 2015 at 13:31
  • @dbush I have added their contents now. Commented Oct 16, 2015 at 13:33
  • 1
    The for loop in the main function seems strange.Why don't you print p only once? Commented Oct 16, 2015 at 13:34

3 Answers 3

2

In main, you probably want to say

for (i = 0; i < MAX; i++)
    {
    printf("%c", p[i]);
    p++;
    }

In order to print each char in p. Since this will output beyond the 0 char, a better way would be to simply say printf("%s", p);, without a loop. Or just printf(p); if you trust the string! Or puts(p); which would apparently print a newline as well, which is most likely desirable for a terminal.

Sign up to request clarification or add additional context in comments.

Comments

2

Your array index, into c, increases every time you go around the loop...instead you only need to change the index to c, when you actually copy a legal character.

for (j = 0, i = 0; b[i] != '\0'; i++)
{
    if (!strchr(a, b[i]))
    {
        c[j] = b[i];
        j++;
        printf("%c", c[j]);
    }
}
c[j] = '\0';

Comments

1

The main problem is here, in letter_remover:

for (i = 0; b[i] != '\0'; i++)
{
    if (!strchr(a, b[i]))
    {
        c[i] = b[i];
        printf("%c", c[i]);
    }
}
c[i] = '\0';

You're using the same index for b and c. So at the end of the loop c contains NULL bytes at the spots where a letter is removed (because the array is static, it is initialized to all zeros). You need to use a separate index for c when you write to it:

for (i = 0, j = 0; b[i] != '\0'; i++)
{
    if (!strchr(a, b[i]))
    {
        c[j] = b[i];
        printf("%c", c[j]);
        j++;
    }
}
c[j] = '\0';

Then main where you're printing the result:

for (i = 0; i < MAX; i++)
{
    printf("%s", p);
    p++;
}

You're printing out the complete string repeatedly starting at each character. So the first time through it print "pl" before it hits a NULL bytes, the the next time it starts at the "l" and prints that again before it hits the NULL byte, and so on.

Once you apply the first fix, all you need to do is print it once:

printf("%s", p);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.