What you want to do is most easily done with a regular expression. However, since you haven't provided a specific string you want corrected, I can't give you a sample regular expression to fix it.
Google "regular expressions" for tutorials.
Remove duplicate characters from a string -- C program?
This sounds like a homework question so I'm not going to write your code for you, but here are a few approaches you could use:
(1) Allocate a string at least as big as the one you are processing. Loop through the source string, and for each character, loop through your destination searching for it. If it is not found, add it to your destination string.
This is inefficient, but it is also "stable" (the order of the characters are preserved).
(2) Loop through the source string and put each character found in an array. Use a sort subroutine on the array. Loop through the array keeping track of each time the character you are looking at changes. Each time there is a change (including the first character), add that character to your destination string.
(3) Allocate an array of booleans the size of the number of entries in the full ASCII table, all initalized to FALSE. Loop through the source string, and for each character retrieve the ASCII value and set that member of the boolean array to TRUE.
When finished you can reconstruct the deduped string from the array using chr(). If you wanted to get really tricky you could make this method stable as well, but I'll leave that as an exercise for you. Hint: array of longs instead of booleans.
Reply:Without writing all the code
String is in s
char *sn = (char *)malloc( strlen( s ) );
char *sx = s
while( *sx != '\0' )
{
if( strchr( sn, *sx ) == 0 )
{
... append *sx to sn
}
sx++
}
Now sn has the filtered string. You can strcpy() it back to s and free sn.
Reply:There are no strings in the c language.
Oh, there are character arrays, but you don't want character arrays, you want strings and there are no strings in the c language.
Reply:void dedupe(char *str)
{
while (*str)
{
if (*str == *(str+1))
strcpy(str, str + 1);
else
str++;
}
}
blazing star
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment