Strspn
int strspn(char *str1, char *str2) { char *s1, *s2; int cnt = 0; for (s1 = str1, s2 = str2; *s1 != '\0' && *s2 != '\0'; s2++) { // s2 ++ every time if (*s1 == *s2) { cnt++; s1++; // s1 ++ only when matched char is found } } return cnt; } int main() { int i; char strtext[] = "129th"; char cset[] = "1234567890000t"; i = strspn (strtext,cset); printf ("The length of initial number is %d.\n",i); return 0; }
Output:
The length of initial number is 4.
page revision: 1, last edited: 21 Apr 2010 06:20