Comparison of STRSPN () function and strcspn () function in C language using _c language

Source: Internet
Author: User

C language Strspn () function: Computes a string of characters in Str that is a string accept
header files: #include <string.h>

The STRSPN () function is used to compute that several consecutive characters in the string str belong to the string accept, and the prototype is:
size_t strspn (const char *STR, const char * accept);

The function Description strspn () computes contiguous characters from the beginning of the parameter str string, all of which are characters in the string accept. Simply put, if STRSPN () returns a value of N, then the string str begins with a sequence of n characters that belong to the character within the string accept.

Return value returns the number of characters in the string accept with the beginning of a sequence of STR literals. So, if Str contains a character that belongs to accept, then the length of the STR is returned and 0 if the first character of Str does not belong to accept.

Note: The retrieved characters are case-sensitive.

Hint: the meaning of Function strcspn () is opposite to strspn (), can compare study.

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
  int i ;
  Char str[] = "129th";
  Char accept[] = "1234567890";
  i = strspn (str, accept);
  printf ("Str first%d characters belong to accept\n", I);
  System ("pause");
  return 0;
}

Execution results:

The first 3 characters of Str belong to accept.

C-language strcspn () function: Computes a string of consecutive characters in Str that are not part of a string accept
header files: #inclued <string.h>

STRCSPN () is used to compute that several consecutive characters in the string str are not part of the string accept, and the prototype is:

  int strcspn (char *str, char *accept);

"Parameter description" str, accept is the two string to be looked up.

STRCSPN () computes contiguous characters from the beginning of the string str, none of which are entirely in the string accept. Simply put, if STRCSPN () returns a value of N, the string str begins with a sequence of n characters that do not contain characters in the string accept.

Return value returns the number of characters in the string str opening sequence that does not contain the string accept.

Note: If none of the characters in Str appear in the accept, the length of the ATR is returned; the retrieved characters are case-sensitive.

Hint: the meaning of Function strspn () is opposite to strcspn (), you can compare learning.

The example returns the position of the same string contained in S1, S2.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
  Char * S1 = "http://c.biancheng.net/cpp/u/biaozhunku/";
  char* s2 = "C is good";
  int n = strcspn (S1,S2);
  printf ("The" "The" the "" The "" The "" The "" the "the" both in S1 and S2 are 
  :%c\n " printf ("The position in S1 is:%d\n", n);
  System ("pause");
  return 0;
}

Run Result:

The "I" both in S1 and S2 is:c the position in
S1

Let's look at an example to determine whether the characters of two strings have duplicates.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
  Char * S1 = "http://c.biancheng.net/cpp/xitong/";
  char* s2 = "Z-+*";
  if (strlen (s1) = = STRCSPN (s1,s2)) {
    printf ("S1 is diffrent from s2!\n");
  else{
    printf ("There is on least one same character in S1 and s2!\n");
  System ("pause");
  return 0;
}

Run Result:

S1 is diffrent from s2!

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.