Remove all duplicates from a given string in c
belongs to collection: String Interview Questions in C, might ask by the interviewer.
All Answers
total answers (1)
belongs to collection: String Interview Questions in C, might ask by the interviewer.
total answers (1)
Steps to check the duplicate character in a given string:
1. Initialize the required variable
pString = “silence is a source of great strength” ;/* input string */
startIndex = 0 ;/* index of next character in input string */
resultIndex = 0; /* index of next character in resultant string */
binTable[255] = {0}; /* Binary table to check the duplicate character */
2. Create a loop that runs until the end of the string (till the null character).
3. We know that every character has the ASCII value, so mark the index of binTable for the unique ASCII value,
for example,
Suppose if input string is pString ,
pString = “silence is a source of great strength” ; /* input string */
Get the character from the string ( pString )
binTableIndex = *(pString + startIndex);
If the character is unique then,
marked binTable for startIndex,
*(pString + startIndex) = MARK_USED;
Copy the *(pString + startIndex) character in resultant string.
*(pString + resultIndex) = *(pString + startIndex);
increment the resultIndex,
resultIndex++;
Increment the startIndex,
startIndex++;
If the character is not unique then,
Only increment the startIndex,
startIndex++;
Again go to read next character until not get the null character.
Assigned the null character in the last of the resultant string to remove the extra character.*(pString+resultIndex) = ‘\0’;
For the better understanding see the below code,
Output: silenc aourfgth
need an explanation for this answer? contact us directly to get an explanation for this answer