#! /usr/bin/env python # def s_digits_count ( s ): #*****************************************************************************80 # ## S_DIGITS_COUNT counts the digits in a string. # # Discussion: # # The string may include spaces, letters, and dashes, but only the # digits 0 through 9 will be counted. # # Example: # # S => 34E94-70.6 # N <= 7 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, the string. # # Output, integer N, the number of digits. # from ch_is_digit import ch_is_digit s_len = len ( s ) s_pos = 0 n = 0 while ( s_pos < s_len ): c = s[s_pos] s_pos = s_pos + 1 if ( ch_is_digit ( c ) ): n = n + 1 return n def s_digits_count_test ( ): #*****************************************************************************80 # ## S_DIGITS_COUNT_TEST tests S_DIGITS_COUNT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'S_DIGITS_COUNT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' S_DIGITS_COUNT counts the digits in a string.' ) print ( '' ) s = '34E94-70.6' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) s = 'Not a one!' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) s = '#8*k >>>> & SEVEN-0.3' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) # # Terminate. # print ( '' ) print ( 'S_DIGITS_COUNT_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) s_digits_count_test ( ) timestamp ( )