Advertisement

Locating Strings

Started by July 14, 2001 02:06 PM
1 comment, last by Indy 23 years, 7 months ago
Hi, Does anyone know how to locate strings? I''ve tried all the different variants in the C++ language, but none work (not for me anyway). So far I have tried experimenting with strstr(mainstr,substr) but I do not achieve the results I want. To give you an idea of what I''m trying to do, I shall do it BASIC. a$="Help me please!!!!" b$="me" for x=1 to len(a$) c$=mid$(a$,x,2):rem the 2 means how many chars to read from x if c$=b$ print"Found you!!":END next I hope that gives you an idea what I''m trying to do. It''s amazing when you try something like this you realise the sort of functions that didn''t make into C/C++. Any help will be greatly appreciated. Thankyou ALL. Indy
Indy
You need a book, this one : http://www.amazon.com/exec/obidos/ASIN/0201379260/ref=lm_lb_6/104-1773197-6899106.


here is how you do it :


There is a nice function find which return the position of the begining of the substring or std::string::npos if it does not find the string :

  //std has been remove for claritystring::size_type string::find( string& const ); //search for a substring and return it''s position or string::npos if it doesn`t find it.string::size_type string::find( string& const, string::size_type idx ); //same as above but start looking at pos id//there is rfind which starts looking by the end of the string//for your problemstring a = "help me please";string b = "me";string::size_type pos;pos = a.find( b );if ( pos != string::npos )       std::cout << "found you"!  
Advertisement
The string functions in C suck, but in the other hand you learn to make your own neat string functions

This topic is closed to new replies.

Advertisement