Summer training at Goeduhub Technologies-Jaipur
Register for Free Certified Workshop on RTU Python Lab (VI SEM):: 01-03-2020 ||  Career options for aspiring CS/ITECEEE or EIC or EEE Engineers
0 like 0 dislike
19 views
in Tutorial & Interview questions by Summer Trainee (6.9k points)

 strspn and strcspn

1 Answer

0 like 0 dislike
by Summer Trainee (6.9k 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.

Related questions

0 like 0 dislike
1 answer
 Go to your Branch CSE or IT | ECE | EE, EIC or EEEMECE
 Know About Popular Colleges/Universities  List of IITsList of NITs | RTU-KOTA | Manipal University-Jaipur | JECRC University | Amity University Jaipur | BIT Mesra-Jaipur | MODY UNIVERSITY | LNMIIT-Jaipur | JK Lakshmipat | Banasthali Vidyapith | POORNIMA University
 Exams:   List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
Placements:  List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Online Free Training:  Artificial Intelligence(AI) & Machine Learning(ML)Python Programming | Internet of Things-IoT | OpenCV (Open Source Computer Vision Library) | LINUX | Big Data : Hadoop | 
 Goeduhub
About Us | Contact Us   Social::   |  | 
...