473,386 Members | 1,647 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,386 software developers and data experts.

Getting substrings in C

I wonder if there is no other way to substract a substring from a
string then just using a while lus. This below works ok, but looks like
a lot of code and statements if I compare it with the use of
std::string. Is this a bit ok? It should get information from a HTTP
request between the chars "GET /" and "HTTP".

Trying to combine <string> and "*.h" C includes and compiling "C++"
instead of just "C" also lead to a disaster, so that alternative does
not look appealing either.

Here is my code:

void getInfo(const char * pszBuf, char * pstrOms, char * pstrAGB, char
* pstrCat, char * pstrPIN)
{
char * afterGETSLASH = strstr(pszBuf, "GET /");
afterGETSLASH = strchr(afterGETSLASH, '[');afterGETSLASH++;

char* pStrHTTP = NULL;
pStrHTTP = strstr(afterGETSLASH, "HTTP");
pStrHTTP--;

int i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrOms[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrAGB[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrCat[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrPIN[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}
}

Dec 23 '05 #1
1 7339
ma*********@hotmail.com wrote:
I wonder if there is no other way to substract a substring from a
string then just using a while lus. This below works ok, but looks like
a lot of code and statements if I compare it with the use of
std::string. Is this a bit ok? It should get information from a HTTP
request between the chars "GET /" and "HTTP".
Yes, there is. However, you're doing a bit more than just getting a
substring.
Trying to combine <string> and "*.h" C includes and compiling "C++"
instead of just "C" also lead to a disaster, so that alternative does
not look appealing either.
Very wise decision. That's a recipe for tears, unless you know what
you're doing - and when you do, the result is C++, not C.
void getInfo(const char * pszBuf, char * pstrOms, char * pstrAGB, char
* pstrCat, char * pstrPIN)
{
char * afterGETSLASH = strstr(pszBuf, "GET /");
afterGETSLASH = strchr(afterGETSLASH, '[');afterGETSLASH++;
Be aware that mixing declarations and code like this can be confusing to
debug unless you're careful, and is not valid in C89 (but is valid C99).
char* pStrHTTP = NULL;
pStrHTTP = strstr(afterGETSLASH, "HTTP");
pStrHTTP--;

int i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrOms[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}


You could do something here involving strcspn() to find the first ',' or
']', pointer comparison to discover whether that's before or after
"HTTP", and then do *pstr0ms='\0' and strncat().

If you know that you will always be able to write to the string, you
could take a leaf out of strtok()'s book, and do *pStrHTTP='\0' before
copying, and *pStrHTTP='H' (or whatever used to be before the 'H', but
be careful of malformed input! Perhaps look for " HTTP" instead. You
don't want to introduce an exploitable buffer overrun.) afterwards. Then
you won't need the pointer comparison. In that case, though, this
writability requirement should go into the function's documentation.

Richard
Dec 23 '05 #2

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

Similar topics

1
by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
3
by: Will McGugan | last post by:
Hi, Is there a simple way of replacing a large number of substrings in a string? I was hoping that str.replace could take a dictionary and use it to replace the occurrences of the keys with the...
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn...
4
by: Robert Dodier | last post by:
Hello all, I'm trying to find substrings that look like 'FOO blah blah blah' in a string. For example give 'blah FOO blah1a blah1b FOO blah2 FOO blah3a blah3b blah3b' I want to get three...
3
by: Girish Sahani | last post by:
Given a length k string,i want to search for 2 substrings (overlap possible) in a list consisting of length k-1 strings. These 2 substrings when 'united' give the original string. e.g given...
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
1
by: Jason S | last post by:
Is there a way to get the position of multiple substrings that match a regexp without using closures? match() returns the substrings themselves, not the positions, and search() seems to only return...
2
by: Pilcrow | last post by:
This problem was raised in comp.lang.perl.misc, and the poster was concerned, among other things, by the speed of execution. Since C is faster than perl, I wonder how a C coder would solve it? ...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.