The problem of maximizing a nonsmooth convex function over an arbitrary set is considered. Based on the optimality condition obtained by Strekalovsky in 1987 an algorithm for solving the problem is proposed. We show that the algorithm can be applied to the nonconvex optimal control problem as well. We illustrate the method by describing some computational experiments performed on a few nonconvex optimal control problems.