Modified code in such a way so that it is generic for both words and characters search in string