Strstr () function source code
/*
Gets the position pointer that contains the S2 string for the first time in S1.
*/
#include <stdlib.h>
char * MY_STRSTR (const char *s1,const char *S2)
{
if (*s1 = = 0)
{
if (*S2)
Return (char *) NULL;
Return (char *) s1;
}
while (*S1)
{
size_t i;
i = 0;
while (1)
{
if (s2[i] = = 0)
{
Return (char *) s1;
}
if (s2[i]! = S1[i])
{
Break
}
i++;
}
s1++;
}
Return (char *) NULL;
}
int main ()
{
Char *str1 = "Ammana_babi";
Char *str2 = "Babi";
Char *p;
if ((p = my_strstr (str1,str2)) = = NULL)
printf ("Can ' t find the string/"%s/"!/n", str2);
Else
printf ("Find the string/"%s/"!/n", p);
STR1 = "ABC";
str2 = "Def";
if ((p = my_strstr (str1,str2)) = = NULL)
printf ("Can ' t find the string/"%s/"!/n", str2);
Else
printf ("Find the string/"%s/"!/n", p);
System ("pause");
return 0;
}
STRPBRK () function source code
/*
Gets the position pointer of the first character in the S1, which is a s2.
*/
#include <stdlib.h>
char * MY_STRPBRK (const char *S1, const char *S2)
{
const char *c = s2;
if (!*S1)
Return (char *) NULL;
while (*S1)
{
for (c = s2; *c; c + +)
{
if (*s1 = = *c)
Break
}
if (*c)
Break
s1++;
}
if (*c = = '/0 ')
S1 = NULL;
Return (char *) s1;
}
int main ()
{
Char *str1 = "Ammana_babi";
Char *str2 = "Babi";
Char *p;
if ((p = my_strpbrk (str1,str2)) = = NULL)
printf ("No same character!/n");
Else
printf ("%c/n", *p);
STR1 = "ABC";
str2 = "Def";
if ((p = my_strpbrk (str1,str2)) = = NULL)
printf ("No same character!/n");
Else
printf ("%c/n", *p);
System ("pause");
return 0;
}
STRCSPN () function source code
/*
Gets the character position of the first character in the S1, which is a s2.
*/
int my_strcspn (const char *S1, const char *S2)
{
const char *s = S1;
const char *p;
while (*S1)
{
for (p = s2; *p; p++)
{
if (*s1 = = *p)
Break
}
if (*p)
Break
s1++;
}
return s1-s;
}
int main ()
{
Char *str1 = "Ammana_babi";
Char *str2 = "Babi";
int offset;
if (offset = my_strcspn (STR1,STR2)) >= strlen (STR1))
printf ("Can ' t find the same character!/n");
Else
printf ("%c/n", * (str1 + offset));
STR1 = "ABC";
str2 = "Def";
if (offset = my_strcspn (STR1,STR2)) >= strlen (STR1))
printf ("Can ' t find the same character!/n");
Else
printf ("%c/n", * (str1 + offset));
System ("pause");
return 0;
}
C Language String processing function source code