debug code

Debug

#include<stdio.h>
#include<conio.h>
void removem(char str[]);

int main()
{
char str[50];

printf("Enter string:");
scanf("%s",str);
removem(str);

return 0;
}

void removem(char str[])
{
int i;
char a[256];

for(i=0;i<256;i++)
a[i]=-1;

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


Can anyone throw some light on this " void removem(char str[]) " function?
I am not able to trace this
if(a[str[i]] == -1)
{
a[str[i]] = i;
printf("%c",str[i]);
}
code.
funny, I could of sworn that's C code, besides, what are you trying to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void removem (char str[])
{
    int i;
    char a[256];

    for (i = 0; i < 256; i++) //okay, set all 256 values of a[] to -1...
        a[i] = -1;

    for (i = 0; str[i] != '\0'; i++) //okay, loop through str until you reach the end...
    {
        if (a[str[i]] == -1) //if the value of a[] at index str[i] is -1, with str[i] returning a char, meaning if str[i] == '7' then if a['7'] == -1, '7' is ASCII is 55, so a[55] == -1.
        {
            a[str[i]] = i; //change a[55] to i
            printf ("%c", str[i]); //print str? why? you haven't changed it
        }
    }
}


also

warning: array subscript has type 'char' [-Wchar-subscripts]|
It displays the string without displaying duplicated characters.

NOTE it does NOT modify the string - it just does not display the duplicate characters.

So if the string parameter is anncfffg then it will display ancfg


Thank you very much Zephilinox & guestgulkan for your valuable explanation.
:)
Last edited on
Topic archived. No new replies allowed.