Suppose you have a string “silence is a source of great strength” when we will remove the duplicate character from the string then the output will be “silenc aourfgth“.
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 */
binTableIndex = *(pString + startIndex);
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++;
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’;
#include <stdio.h> #define SIZE_BIN_TABLE 256 #define UNIQUE_CHARACTER 0 #define MARK_USED 1 /* Removes duplicate characters from the Input string */ char *RemoveDupChar(char *pString) { short binTable[SIZE_BIN_TABLE] = {0}; //Bin table int startIndex = 0, resultIndex = 0; // Index unsigned char binTableIndex = 0; while (*(pString + startIndex)) //Till not get null character { binTableIndex = *(pString + startIndex); //get character from startIndex if (binTable[binTableIndex] == UNIQUE_CHARACTER) //check unique character { binTable[binTableIndex] = MARK_USED; //Marked the binTableIndex *(pString + resultIndex) = *(pString + startIndex); //copy character in result string resultIndex++; } startIndex++; } *(pString+resultIndex) = '\0'; //Assign null character to remove extra character return pString; } int main() { char pString[] = "silence is a source of great strength"; //input string char *pResultString = RemoveDupChar(pString); //Remove duplicate printf("%s", pResultString ); //print result string return 0; }
Output: silenc aourfgth
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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