String matching: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 87: | Line 87: | ||
MsgBox, "%String1%" ends with "%String2%". |
MsgBox, "%String1%" ends with "%String2%". |
||
</lang> |
</lang> |
||
=={{header|C|C}}== |
|||
Case sensitive matching.......... |
|||
<lang C> |
|||
#include<string.h> |
|||
#include<stdio.h> |
|||
#define true 1 |
|||
#define false 0 |
|||
int startsWith(char* container,char* target) |
|||
{ |
|||
int i,targetLength = strlen(target),sourceLength = strlen(container); |
|||
if(targetLength<=sourceLength) |
|||
{ |
|||
for(i=0;i<targetLength;i++) |
|||
{ |
|||
if((target[i]!=container[i])) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
int endsWith(char* container,char* target) |
|||
{ |
|||
int i,targetLength = strlen(target),sourceLength = strlen(container); |
|||
if(targetLength<=sourceLength) |
|||
{ |
|||
for(i=sourceLength-targetLength;i<sourceLength;i++) |
|||
{ |
|||
if(container[i]!=target[i-(sourceLength-targetLength)]) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
int doesContain(char* container,char* target) |
|||
{ |
|||
int i,j,targetLength = strlen(target),sourceLength = strlen(container); |
|||
if(targetLength<=sourceLength) |
|||
{ |
|||
for(i=0;i<sourceLength;i++) |
|||
{ |
|||
if((container[i]==target[0])&&((i+targetLength)<=sourceLength)) |
|||
{ |
|||
for(j=0;j<targetLength;j++) |
|||
{ |
|||
if(container[i+j]!=target[j]) |
|||
break; |
|||
} |
|||
if(j==targetLength) |
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
int main() |
|||
{ |
|||
printf("Starts with Test ( Hello,Hell ) : %d",startsWith("Hello","Hell")); |
|||
printf("\nEnds with Test ( Code,ode ) : %d",endsWith("Code","ode")); |
|||
printf("\nContains Test ( Google,msn ) : %d",doesContain("Google","msn")); |
|||
return 0; |
|||
} |
|||
</lang> |
|||
<out> |
|||
Starts with Test ( Hello,Hell ) : 1 |
|||
Ends with Test ( Code,ode ) : 1 |
|||
Contains Test ( Google,msn ) : 0 |
|||
</out> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |