473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help getting substring

Hi there,
I need a program to extract a substring from the following pattern

xxxx<XXXXX>xxxx

therer, x and X are any character and the string between < and is
what I need. I use the following program to extract the substring, but
something wrong

char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
memcpy( lpBuf, p, q-p );
return lpBuf;
}

apply the function to some string like xxxx<http://www.abc.com>xxxx

what I get is "http://www.abc.comm"

I am wondering where is the double m come?

Thanks in advance

Jul 16 '06 #1
8 2797
Hello,

See comments inline (not tested):

gm****@21cn.com wrote:
char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
Compare with null AFTER addition of 1?
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
Modify const char * ?
memcpy( lpBuf, p, q-p );
q-p+1?
return lpBuf;
}
Kidn regards.

Jul 16 '06 #2
gm****@21cn.com wrote:
Hi there,
I need a program to extract a substring from the following pattern

xxxx<XXXXX>xxxx

therer, x and X are any character and the string between < and is
what I need. I use the following program to extract the substring, but
something wrong

char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
memcpy( lpBuf, p, q-p );
Better to use memmove since p points inside lpBuf.
You missed the terminating nul.use memmove( lpBuf, p, q-p+1 );
return lpBuf;
}

apply the function to some string like xxxx<http://www.abc.com>xxxx

what I get is "http://www.abc.comm"

I am wondering where is the double m come?

Thanks in advance
Jul 16 '06 #3
gm****@21cn.com wrote:
char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
Careful. You just cast away the const.
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0';
Oooohh... You just wrote to read-only memory. Clearly,
you didn't mean to declare lpStr as const.
memcpy( lpBuf, p, q-p );
Ackk! You are really blowing away lpStr!! Is
this intentional?
return lpBuf;
}
Jul 16 '06 #4
gm****@21cn.com wrote:
Hi there,
I need a program to extract a substring from the following pattern

xxxx<XXXXX>xxxx

therer, x and X are any character and the string between < and is
what I need. I use the following program to extract the substring, but
something wrong

char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
memcpy( lpBuf, p, q-p );
return lpBuf;
}

apply the function to some string like xxxx<http://www.abc.com>xxxx

what I get is "http://www.abc.comm"

I am wondering where is the double m come?

Thanks in advance
I would do something like

void get_bracketed_string(const char s[], char result[])
{
char *langle, *rangle;

langle = strchr(s, '<');
rangle = strchr(s, '>');
if ((langle == NULL) || (rangle == NULL) || (langle rangle)) {
result = "";
} else {
strncpy(result, langle + 1, rangle - langle - 1);
result[rangle - langle - 1] = '\0';
}
}
August
Jul 16 '06 #5
August Karlstrom wrote:
I would do something like

void get_bracketed_string(const char s[], char result[])
{
char *langle, *rangle;
I'd keep these as const char * to reinforce that you still can't modify
the contents even though strchr has returned a non-const pointer.
langle = strchr(s, '<');
rangle = strchr(s, '>');
if ((langle == NULL) || (rangle == NULL) || (langle rangle)) {
result = "";
There's no point modifying the parameter! The caller won't notice any
change. You'd better change it to this:
result[0] = '\0';
} else {
strncpy(result, langle + 1, rangle - langle - 1);
result[rangle - langle - 1] = '\0';
}
}
August
Jul 16 '06 #6
Thank you all of you. I've got it

Jul 16 '06 #7
gm****@21cn.com wrote:
Hi there,
I need a program to extract a substring from the following pattern

xxxx<XXXXX>xxxx

therer, x and X are any character and the string between < and is
what I need. I use the following program to extract the substring, but
something wrong

char* deangle(const char* lpStr)
{
char *lpBuf = (char *)lpStr;
char *p = strchr( lpBuf, '<' ) + 1;
if (!p) return NULL;
char *q = strchr( lpBuf, '>' );
if (!q) return NULL;
*q = '\0'; /* to make sure no extra characters at the end of the
substring
memcpy( lpBuf, p, q-p );
return lpBuf;
}
In addition to the various points made so far you also have a big
logic error. What does deangle do when passed a string of the form
"xxx>XXXX<xxx"?

So as not to be accused of being too negative, I would propose:

char *deangle(char *str)
{
char *open = strchr(str, '<');
if (!open)
return NULL;
char *close = strchr(open + 1, '>');
if (!close)
return NULL;
int sublen = (close - open) - 1; /* will be >= 0 */
memmove(str, open + 1, sublen);
str[sublen] = '\0';
return str;
}

or, in older C, and in the single-entry-exit style:

char *deangle(char *str)
{
char *close;
char *open = strchr(str, '<');
if (open && (close = strchr(open + 1, '>'))) {
int sublen = (close - open) - 1; /* will be >= 0 */
memmove(str, open + 1, sublen);
str[sublen] = '\0';
}
else str = NULL;
return str;
}

but I would argue against the idea of returning a pointer to the same
(clobbered) data. What if the string has two <...parts and you
later want both?

--
Ben.
Jul 16 '06 #8
Simon Biber wrote:
August Karlstrom wrote:
>I would do something like

void get_bracketed_string(const char s[], char result[])
{
char *langle, *rangle;

I'd keep these as const char * to reinforce that you still can't modify
the contents even though strchr has returned a non-const pointer.
OK.
> langle = strchr(s, '<');
rangle = strchr(s, '>');
if ((langle == NULL) || (rangle == NULL) || (langle rangle)) {
result = "";

There's no point modifying the parameter! The caller won't notice any
change. You'd better change it to this:
result[0] = '\0';
Yes of course, thanks.

Now we have:

void get_bracketed_string(const char s[], char result[])
{
const char *langle, *rangle;

langle = strchr(s, '<');
rangle = strchr(s, '>');
if ((langle == NULL) || (rangle == NULL) || (langle rangle)) {
result[0] = '\0';
} else {
strncpy(result, langle + 1, rangle - langle - 1);
result[rangle - langle - 1] = '\0';
}
}
August
Jul 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
10
by: Learner | last post by:
Hello, I am trying to create few dynamic controls and once they are rendered I need to save the information that was entered into these dynamic fileds. For instance when I create 3 radio button...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
2
by: garyusenet | last post by:
Hi All, I have been working on the following programme over the last day or so and have made a good deal of progress. It is a very simple programme, but is proving very useful as a learning aid,...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
3
by: tshad | last post by:
I have a file that I converted from VB.Net to C# that works fine in VB.Net when I compile but not in C# using the same libraries. The error I am getting is: PageInit.cs(9,7): error CS0138: A...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.