Solved

regular expression to discard consecutive consonants

Posted on 2004-12-01
334 Views
how would I write a regular expression that would eliminate consecutive consonants from a word and spit out the rest of the word minus the deleted consonants? ie hello would become helo, butterfly would become buter?

thanx!
0

LVL 4

Expert Comment

you will need to loop through each character within the string and test if the next character in the same string is the same character, if so, skip to next character else write the tested character to a new string

ie:

char * stripDuplicates(char *source, char *target)
{
// Loop through all characters in source.
// Is current character in source string equal following character (current character  + 1) in source string
// If not the same then add current character to target string

return target;
}
0

Author Comment

can I grab individual characters from yytext? the way i understand it, is yytext holds the token found from a regular expression...so if the token is the word "friends" then can I assume yytext[0] contains "f", yytext[1] contains "r" etc?
0

LVL 4

Expert Comment

I am not entirely sure what yytext is but I am guessing it is a char *.  In this case, each individual character can be accessed either by:

yytext[position]

or by

*(yytext + position)

either method will return the indivdual character at position, where 0 is the first character.

Eg:

yytext[0] = 'f'
yytext[1] = 'r'

*yytext = 'f'
*(yytext + 1) = 'r'

etc...
0

LVL 16

Accepted Solution

you have to run a loop like

for(i=0;i<strlen(yytext)-1;i++)
{
if "yytext[i] is a consonant" && "yytext[i+1] is a consonant"
//left shift yytext array from i+1 th position till the last
}

Manav
0

LVL 9

Expert Comment

>> left shift yytext array from i+1 th position till the last
that's a complex and expensive operation. I suggest maitaining one index for reading and one for writing.
0

Author Comment

thanx guys! it seems to work! :)
0

LVL 16

Expert Comment

> that's a complex and expensive operation. I suggest maitaining one index for reading and one for writing.

suits me just fine, if it suits the author.

Manav
0

Featured Post

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.