Jump to content

String matching: Difference between revisions

→‎{{header|C}}: Condensed in observation of the "Know thy standard library" commandment. Output fixed to put newlines at the end of lines.
(→‎{{header|Euphoria}}: Euphoria example added)
(→‎{{header|C}}: Condensed in observation of the "Know thy standard library" commandment. Output fixed to put newlines at the end of lines.)
Line 126:
=={{header|C}}==
Case sensitive matching:
<lang C>#include <string.h>
#include <stringstdio.h>
#include<stdio.h>
 
int startsWith(char* container, char* target)
#define true 1
#define false 0
 
int startsWith(char* container,char* target)
{
int i,targetLength size_t clen = strlen(targetcontainer),sourceLength tlen = strlen(containertarget);
if (clen < tlen)
return true0;
if(targetLength<=sourceLength)
return strncmp(container, target, tlen) == 0;
{
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 size_t clen = strlen(targetcontainer),sourceLength tlen = strlen(containertarget);
if (clen < tlen)
return false0;
if(targetLength<=sourceLength)
return strncmp(container + clen - tlen, target, tlen) == 0;
{
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)
{
return strstr(container, target) != 0;
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(void)
{
printf("Starts with Test ( Hello,Hell ) : %d\n", startsWith("Hello","Hell"));
printf("\nEndsEnds with Test ( Code,ode ) : %d\n", endsWith("Code","ode"));
printf("\nContainsContains Test ( Google,msn ) : %d\n", doesContain("Google","msn"));
 
return 0;
}</lang>
}
</lang>
Output :
<pre>Starts with Test ( Hello,Hell ) : 1
<lang>
Starts with Test ( Hello,Hell ) : 1
Ends with Test ( Code,ode ) : 1
Contains Test ( Google,msn ) : 0</pre>
</lang>
Code without using string library to demonstrate how char strings are just pointers:
<lang C>#include <stdio.h>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.