C Language String processing function source code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.