match what kind of bracket encloses a string and delete it
I am trying to get rid of the brackets around a string. For example: I can have (This string) or {This String} or [This String] or +This String+. I want to return just This String (without any kind of brackets or +s).
开发者_开发知识库I have spent so much time around regex and have given up. If you can help, it would be great.
Here is one possible way to do it. Not necessarily the most efficient and without complete error checking.
Edit Updated based on comment to another post that makes it sound as if the brackets can be anywhere in the string. This removes matching all matching brackets as long as there is a matching closing bracket for all of them. It does not remove them all if a closing bracket is not found. The OP needs to do a bit of the homework. It also does not check for proper balancing of brackets, which it is not clear to me if that is a requirement.
// return 1 if a bracket is found
int RemoveBracket( char *str )
{
char openbracket[] = {'[', '(', '{', '+', '\0' };
char closebracket[] = {']', ')', '}', '+', '\0' };
int start, end, j;
char match = 0;
// find open bracket
start = 0;
for ( ; str[start] && !match; start++ ) {
for ( j = 0; openbracket[j] && !match; j++ ) {
if ( str[start] == openbracket[j] )
match = closebracket[j];
}
if ( match )
break;
}
if ( match == 0 )
// no open bracket found
return 0;
// find closing bracket
end = start + 1;
for ( ; str[end]; end++ ) {
if ( str[end] == match )
break;
}
if ( !str[end] )
// no closing bracket found
return 0;
// remove them
memmove( str + start, str + start + 1, end - start - 1 );
memmove( str + end - 1, str + end + 1, strlen( str + end ) );
return 1; // since we found one
}
void RemoveBrackets( char *str )
{
// remove matching brackets.
while ( RemoveBracket(str ))
;
}
int main( int argc, char* argv[] )
{
char str[50];
strcpy( str, "a[b(c)de]" );
RemoveBrackets( str );
printf( "%s\n", str );
strcpy( str, "{not bracketed" );
RemoveBrackets( str );
printf( "%s\n", str );
strcpy( str, "(paren)s" );
RemoveBrackets( str );
printf( "%s\n", str );
strcpy( str, "abc+def+{gh}i" );
RemoveBrackets( str );
printf( "%s\n", str );
}
I found a easier way to handle this:
Go thro' each character in the string, if it matches {[(<+_->)]}
, then replace that char by a space and then trim away the space later.
Thanks a lot for all your help.
Here's my solution:
int main(int argc, char *argv)
{
char *string = "abc [def] ghi";
char *final = (char *) malloc(strlen(string) + 1);
int length = 0;
int count = 0;
// process every single character of string
for(count; count <= strlen(string); count++) {
// check if it has to be copied or not
if(string[count] != '[' && string[count] != ']') {
// if it has, copy it and increase length of final
final[length] = string[count];
length += 1;
}
}
// then 'throw away' unused memory
final = (char *) realloc((void *) final, length);
printf("%s\n", final);
}
And here's the output:
blackbear@blackbear-laptop:~$ ./prova.out
abc def ghi
Then add your I/O code and the type of brackets it has to check via if..else if..else if..
or switch(string[count])
or again using an array and replace my if
with a loop that checks every type of bracket.
It is pretty straightforward with a c string.
char s[] = "{test}";
size_t t = strlen(s) - 2;
memmove(s, s + 1, t);
s[t] = '\0';
char currentChar,initChar;
initChar=myString[0];
//You should check if exits
currentChar=myString[1];
string noBracketString;
int cont=0;
while(currentChar!=initChar){
noBracketString+=currentChar;
cont++;
currentChar=myString[cont];
}
//String without brackets
return noBracketString;
Hope Helps!
精彩评论