Rewrite the string operation functions-strspns and strcspns in string. h.

Source: Internet
Author: User

Function prototype: size_t strspn (const char * string, const char * strcharset)
Return Value Description: returns an integer that specifies the number of characters in the strcharset string. If 0 is returned, the specified character is not found in the string.
Parameter description:
String: String ending with '/0.
Strcharset: the specified string, ending with '/0.
Function Description:
It is used to calculate the number of specified characters (strcharset) in the characters starting with a string.
Example:


Char strings [] = "1234 ";
Char strcharset [] =: 01234567 ";
Int Len = strspn (string, strcharset); // The LEN value is 4;
Char string2 [] = "8123 ";
Int len2 = strspns (string, strcharset); // The value of len2 is 0.
Char string3 [] = "128676 ";
Int len3 = strspns (string, strcharset); // The value of len3 is 2.


Function implementation:

Size_t strspn (const char * string, const char * strcharset)
{
Assert (null! = String) & (null! = Strcharset ));
Const char * P;
Const char * s;
Size_t COUNT = 0;
For (P = string; * P! = ''; P ++)
{
For (S = strcharset; s! = '/0'; s ++)
{
If (* P = * s)
Break;
}
If (* A = '/0 ')
Return count; // exit if the specified string cannot be found.
Count ++; // otherwise, add count to 1 and continue execution.
}

Return count;
}


Function prototype: size_t strcspn (const char * string, const char * substrset)
Return Value Description: returns an integer that specifies the number of characters starting with a string that are not in the specified string. If the first character of string is in the string substrset, 0 is returned.
Parameter description, which is the same as strspns.
Function Description:
Strcspns are the opposite of strspns. It calculates the number of characters starting with a string that are not in the specified string (that is, substrset. In strcspns, c Represents complement (Supplement ).
Example:

Const char * STR = "hello ";
Const char * substrset = "Lo ";
Int Len = strcspn (STR, substrset); // The LEN value is 2.
Const char * strset2 = "H ";
Int len2 = strcspn (STR, strset2); // The LEN value is 0.
Const char * strset3 = ""; // if the specified string is an empty string, only the terminator'

Function implementation:
Size_t strcspn (const char * STR, const char * strset)
{
Assert (null! = Str) & (null! = Strset ));
Const char * left_str;
Const char * right_str;
Size_t COUNT = 0;
For (left_str = STR; * left_str! = '/0'; left_str ++)
{
For (right_str = strset; * right_str! = '/0'; right_str ++)
{
If (* left_str = * right_str)
{
Break;
} // Exit if the same character is found
}
If (* left_str = * right_str)
{
Return count;
} // If the same character exists, the search ends and the result is returned.
Count ++;
}
Return count;
}

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.