Jump to content

String matching: Difference between revisions

no edit summary
No edit summary
Line 10:
A) Print the location of the match for part 2
B) Handle multiple occurrences of a string for part 2.
 
=={{header|C++}}==
 
<lang cpp>
#include <string>
 
string s1="abcd";
string s2="abab";
string s3="ab";
//Beginning
s1.compare(0,s3.size(),s3)!=0;
//End
s1.compare(s1.size()-s3.size(),s3.size(),s3)!=0;
//Anywhere
s1.find(s2)//returns string::npos
int loc=s2.find(s3)//returns 0
loc=s2.find(s3,loc+1)//returns 2
</lang>
 
=={{header|Python}}==
Line 16 ⟶ 34:
"abcd".startswith("ab")#returns true
"abcd".endswith("zn")#returns false
locationloc="abab".find("abbb")#returns 0-1
locationloc="abab".find("ab",location+1)#returns 20
loc="abab".find("ab",loc+1)#returns 2
</lang>
10

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.