M
M
MadWastefield2017-10-11 23:37:34
C++ / C#
MadWastefield, 2017-10-11 23:37:34

Is it possible in C++ to find out the position from which a substring starts without iterating through the characters in the string?

there is a string char str[50] and a substring "tt", is it possible to somehow find out the position of a substring in a string without using the string library and character-by-character enumeration of the string str?
There is strstr () which returns a pointer to the beginning of the substring in the string str and in a separate "laboraton" case, when everything is launched in a separate program in order to only check this, the following method works for me:

char *pSubStr = strstr(str, "s"); 
int num = str - pSubStr;

the int variable turns out to be exactly what is needed, but as soon as I try to apply it in a real program, where the string is set to the devil where, and the substring is taken after about a hundred lines of code, it turns out that the difference between the strings str - pSubStr gives out not the position of the substring, but a huge number. Apparently the method does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-10-12
@MadWastefield

1. You need to check pSubStr for NULL. If so, it was not found.
2. Quite the contrary, pSubStr - str.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question