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 purpose of void pointer?

570


Write a program to replace n bits from the position p of the bit representation of an inputted character x with the one's complement. Method invertBit takes 3 parameters x as input character, p as position and n as the number of positions from p. Replace n bits from pth position in 8 bit character x. Then return the characters by inverting the bits.

3664


What is function in c with example?

604


Why do we use null pointer?

582


A banker has a seif with a cipher. Not to forget the cipher, he wants to write it coded as following: each digit to be replaced with the difference of 9 with the current digit. The banker chose a cipher. Decipher it knowing the cipher starts with a digit different than 9. I need to write a program that takes the cipher from the keyboard and prints the new cipher. I thought of the following: Take the input from the keyboard and put it into a string or an array. Go through the object with a for and for each digit other than the first, substract it from 9 and add it to another variable. Print the new variable. Theoretically I thought of it but I don't know much C. Could you give me any kind of hint, whether I am on the right track or not?

1485






Why cant I open a file by its explicit path?

570


What are the basic data types associated with c?

785


Explain null pointer.

593


What is assignment operator?

603


Why dont c comments nest?

596


C language questions for civil engineering

1224


What is getch c?

832


Differentiate abs() function from fabs() function.

574


Is return a keyword in c?

571


What is extern storage class in c?

486