Position of a String within a String

D

Daniel Bohner

I've been banging my head against a wall for a day now - I just can't
get the syntax right...

I'm trying to bring back the position of a string within a string -
I'm just not that good of a C guy (yet).

My concept:
jdeStripTrailingBlanks(lpDS->szString);
jdeStripTrailingBlanks(lpDS->szStringToFind);

lpDS->szCharPositionFound = strstr(lpDS->szString,
lpDS->szStringToFind);

My lack of knowledge:
What datatype does szCharPositionFound need to be?
Do I need to convert the szCharPositionFound to an Integer? (I need to
provide to the calling program as an integer)

Am I on the right track or am I so far off that I should go back to
shoveling snow?

If anyone has an example - I'd love to see it..

Daniel
 
J

Julie J.

What datatype does szCharPositionFound need to be?

char *
Do I need to convert the szCharPositionFound to an Integer? (I need to
provide to the calling program as an integer)

To convert to an integer, you need to perform pointer arithmetic --
fortunately, it is pretty much the same as regular arithmetic. The following
just subtracts the found position from the start position, the result is
0-based index of the substring in the string:

int index = szCharPositionFound - lpDS->szString;

And presuming that you want to signal the condition where the substring doesn't
appear in the string:

if (szCharPositionFound != NULL)
return (szCharPositionFound - lpDS->szString); // return 0-based index
else
return -1; // -1 means substring not found
 
S

Sharad Kala

Daniel Bohner said:
I've been banging my head against a wall for a day now - I just can't
get the syntax right...

I'm trying to bring back the position of a string within a string -
I'm just not that good of a C guy (yet).

My concept:
jdeStripTrailingBlanks(lpDS->szString);
jdeStripTrailingBlanks(lpDS->szStringToFind);

lpDS->szCharPositionFound = strstr(lpDS->szString,
lpDS->szStringToFind);

What is lpDS? I think you need to be clearer in your question.
My lack of knowledge:
What datatype does szCharPositionFound need to be?
Do I need to convert the szCharPositionFound to an Integer? (I need to
provide to the calling program as an integer)

Am I on the right track or am I so far off that I should go back to
shoveling snow?

C++ std::string makes doing things much easier.
#include <iostream>
#include <string>

int main ()
{
std::string s1 = "hello";
int i1 = s1.find ( "ll" );
std::cout << i1 << "\n"; // Print 2
}
 
J

John Harrison

Daniel Bohner said:
I've been banging my head against a wall for a day now - I just can't
get the syntax right...

I'm trying to bring back the position of a string within a string -
I'm just not that good of a C guy (yet).

C? This is a C++ group, perhaps you should try comp.lang.c.
My concept:
jdeStripTrailingBlanks(lpDS->szString);
jdeStripTrailingBlanks(lpDS->szStringToFind);

lpDS->szCharPositionFound = strstr(lpDS->szString,
lpDS->szStringToFind);

My lack of knowledge:
What datatype does szCharPositionFound need to be?

Either char* or const char*, depending on whether you are programming C or
C++ and on what exactly your situation is (e.g. the type of lpDS->szString)
Do I need to convert the szCharPositionFound to an Integer? (I need to
provide to the calling program as an integer)

Well then I guess you do need to convert

int offset = lpDS->szCharPositionFound - lpDS->szString;

but be aware that you should test lpDS->szCharPositionFound first, if you
find nothing then lpDS->szCharPositionFound will be NULL.
Am I on the right track or am I so far off that I should go back to
shoveling snow?

If anyone has an example - I'd love to see it..

Daniel

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top