GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials ||  MACHINE LEARNING || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
Register For Online Free Certified Workshops 
0 like 0 dislike
43 views
in Tutorial & Interview questions by Goeduhub's Expert (8k points)

 strspn and strcspn

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8k points)
 
Best answer
Given a string, strspn calculates the length of the initial substring (span) consisting solely of a specific list of characters. strcspn is similar, except it calculates the length of the initial substring consisting of any characters except those listed:

/*  Provided a string of "tokens" delimited by "separators", print the tokens along  with the token separators that get skipped. */

#include <stdio.h>

#include <string.h>

int main(void)

{    

const char sepchars[] = ",.;!?";    

char foo[] = ";ball call,.fall gall hall!?.,";    

char *s;    

int n;

for (s = foo; *s != 0; /*empty*/)

{                   /* Get the number of token separator characters. */        

n = (int)strspn(s, sepchars);

if (n > 0)            

printf("skipping separators: << %.*s >> (length=%d)\n", n, s, n);

        /* Actually skip the separators now. */        

s += n;

        /* Get the number of token (non-separator) characters. */        

n = (int)strcspn(s, sepchars);

if (n > 0)            

printf("token found: << %.*s >> (length=%d)\n", n, s, n);

        /* Skip the token now. */        

s += n;    

}

printf("== token list exhausted ==\n");

return 0;

}

Analogous functions using wide-character strings are wcsspn and wcscspn; they're used the same way.
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...