Write a program for deleting duplicate elements in an array

Answer Posted / sandeep ambekar

//
// MAIN -- Write a program for deleting duplicate elements in an array
// Example: # a.out string substr
// I/P example: ./a.out Bangalore a
//
// Author -- Sandeep Ambekar

#include <stdio.h>
#define TRUE 1
#define FALSE 0

void
purge_char_from_string (char *str, char *c)
{
int i, k, flag, count; // i/j for index, flag to
keep track of
// 'c' and count for iteration..
char *buf = str; // save the original string
start:
str = buf; // we could enter the loop
again.
i = 0;
k = 1;
count = 0;
flag = FALSE; // INIT variables...
printf ("Input String [%s] [%c] \n", str, *c);

while (str[i] != '\0')
{
printf ("\t (%c) <==> (%c)\n", str[i], str[k]);
if (str[i] == c[0])
{
str[i] = str[k];
flag = TRUE;
count++;
}
else if (flag == TRUE)
{
str[i] = str[k]; // later have a while loop
to find a char
// to which is !c and replace them.
}
i++;
k++;
}
printf ("[%s]\n", buf);
if ((count - 1) >= 1)
goto start;
} // end of
purge_char_from_string ....

//
// MAIN -- Program to Purge a given character from a string.
//

int
main (int argc, char *argv[])
{
if (argc < 3)
{
printf ("Input <String> <char> \n");
return 1;
}
printf (" ## Input string %s : Char [%s]\n", argv[1],
argv[2]);
purge_char_from_string (argv[1], argv[2]);

printf (" Trimmed String ---> %s\n", argv[1]);
return 0;
}

Is This Answer Correct ?    5 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the correct code to have following output in c using nested for loop?

696


What are run-time errors?

689


What is table lookup in c?

726


Disadvantages of C language.

740


Implement bit Array in C.

780






Explain indirection?

740


What is pragma c?

728


Explain how can I right-justify a string?

714


How do you print only part of a string?

695


What type of function is main ()?

687


Explain how do you determine a file’s attributes?

693


Write a function stroverlap that takes (at least) two strings, and concatenates them, but does not duplicate any overlap. You only need to worry about overlaps between the end of the first string and the beginning of the second string. Examples: batman, manonthemoon = batmanonthemoon batmmamaman, mamamanonthemoon = batmmamamanonthemoon bat, man = batman batman, batman = batman batman, menonthemoon = batmanmenonthemoon

1832


Explain what are reserved words?

725


What are the 5 types of organizational structures?

659


What is difference between union and structure in c?

673