search substring from a string
I am stuck in some logic and want to write a program to do following tasks :
i have three string variables to be compared to each other,a string having sub string & hierarchy string!!
1.) name1=john name2=tom_john_tom name3=alextom_john
thus we need to search john from name2 and name3 and if name1 exists in both name2 and name3 then ok else check for step2
2.) name1=a.b.c.d ,name2=a.b.c.d and name3=a.b.c.d
we need to compare each string seperated by a dot in all three variables and we need to match each string seperated by a delimeter "."
if name1.a==name2.a==name3.a and name1.b==name2.b==name3.b ,name1.c==name2.c==name3.c and name1.d==name2.d==name3.d then its a match
else its a mismatch
Also,the catch is we can have name1 ,name2 and name3 in format name1=*.*.*.* and name2=*.*.*.* and name3=*.*.*.*
where * defines it can be any value to be matched
If operator wants to match only the second field , then the logic should be like:
If the Value is to be matched = (*.#.*.*)
its a mismatch
# => for this field of name2 and name3 shall be same
* => for this field name2 and name3 shall be ignored for comparison
to obtain option1 we can have find function and substr however pls suggest how to approach for second option !!!!
since i am a beginner it would be of great help thanks inadvance !!!
Re: search substring from a string
If I understand you correctly you've got part 1 sorted.
The first part of part 2 is easy enough. Just compare name1 with name2, and name2 with name3, and if both equality tests are true then the three strings are identical.
The second part is a little more involved, but not difficult. First you need to parse the *.*.#.* string (let's call it str) to find out which parts are * and which parts are #. This could be as easy as just looking at str, str, str and str, and I'd probably do it as follows:
For the comparison, one approach could be to count the dots to find the location of substrings, then use strncmp to compare a fixed length string; let's say we've worked out for the 3rd part of name1 that name1part3start=17 and n1len=6 (count the characters: name1="government.india.public.property")
and to make things a bit more difficult let's say name3=rural.india.public.sales, so name3part3start=12 (we don't really need the length, although obviously if n1len!=n3len then the strncmp will also fail), we would then do:
name1 into "government" "india" "public" "property" (let's call this vec1). There's probably an STL function to do this. Similarly let's split name3 into "rural" "india" "public" "sales" and call it vec3, then we just do:
|All times are GMT +5.5. The time now is 11:31.|