Solved

# One last little question about strings and indexes

Posted on 2005-04-10
Medium Priority
252 Views
I still need to know the index of a string found within another string.

If the string's value were "123456789" and I was looking for the position of "789", I need a function which will return the value 6.

This will help me close the loop and finish what I've asked seveal related question tonight.

Thanks,
Bob

0
Question by:ba272
• 5
• 3

LVL 32

Assisted Solution

r-k earned 800 total points
ID: 13750472
I guess I came in late and missed your other questions. If you're talking about the string class, then something like this:

string str="123456789";
int iPos=str.find("789",0);   // Look for "789" starting at location 0 in str

http://www.msoe.edu/eecs/cese/resources/stl/string.htm

as well as other places.
0

LVL 30

Accepted Solution

Axter earned 1200 total points
ID: 13750476
>>If the string's value were "123456789" and I was looking for the position of "789", I need a function which will return the value 6.

Do you need this for C-Style string, or C++ string?

In either case, if you're just looking for specific character within the string, you can use operator[] to get it.

char c = Data[5]; //Index 6

The above code works for both C-Style string and std::string.
0

LVL 30

Expert Comment

ID: 13750486
>>I need a function which will return the value 6.

What I posted in your first question would be the write approach.
Remember, that in C/C++ the index is a zero based index.

size_t p = mystring.find("789"); //Return 5

If you need it to be six (1 base index), then just add 1 to it.
size_t p = mystring.find("789") + 1;

0

Author Comment

ID: 13750487
I guess I work in C-style character arrays when using C++, so I think I was missing out on a lot of capabilities all those years.
0

LVL 30

Expert Comment

ID: 13750498
If you want to use C-Style string, then you can index it by comparing it to your original pointer.

char str[] = "123456789";

char *p = strstr(str, "789");

int pos = p-str; //This will give you 5
int pos1 = (p-str) + 1; //This will give you 6 for a 1 based index
0

LVL 32

Expert Comment

ID: 13750499
If you're talking about a C-style zero-terminated char array, then:

char *psz="123456789";
char *psz2=strstr(psz,"789");
int iPos=psz2-psz;                      // should return 6

(I am writing the above from memory and don't use this much, so please verify a bit)
0

LVL 30

Expert Comment

ID: 13750508
FYI:
char *psz="123456789";

I don't recommend using a non-constant pointer to point to a string literal.
Changing the contents of a string literal is consider undefined behavior in C++.
So it's better to make it a constant, that way you avoid accidently modifying it's contents.
0

LVL 32

Expert Comment

ID: 13750523
You're right, I was just being a bit lazy for purpose of illustration.

I am puzzled why you are adding 1 to get 6. I would expect

int pos = p-str;

to return 6 in your example.
0

LVL 30

Expert Comment

ID: 13750537
>>I am puzzled why you are adding 1 to get 6. I would expect
>>int pos = p-str;

You're right.  I didn't test it out.  My C string logic is a little rusty.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, whâ€¦
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month12 days, 15 hours left to enroll