Using strspn:
#include <iostream> #include <string.h> using namespace std; int main() { char *string1 = "The value is 3.14159"; char *string2 = "test "; cout << "string1 = " << string1 << "\nstring2 = " << string2 << "\n\nThe length of the initial segment of string1\n" << "containing only characters from string2 = " << strspn( string1, string2 ) << endl; return 0; }