ucwords()

# ucwords() -- Uppercase the first character of each word in a string
# Usage: ucwords string
# depend: strtoupper(): //www.ronggeng.net/index.php/2009/04/24/strtoupper/
#
# Example:
#
# ucwords "test test test";
#

ucwords() {
	[ $# -eq 1 ] || return 1;
	! type -t strtoupper &>/dev/null && return 1;
	local _x _c _p _ret="" _str="$1";
	_p=0;
	for ((_x=0;_x<${#_str};_x++)); do
		_c=${_str:$_x:1};
		if [ "$_c" != " " ] && [ "$_p" = "0" ]; then
			_ret+="$(strtoupper "$_c")";
			_p=1;continue;
		fi;
		[ "$_c" = " " ] && _p=0;
		_ret+="$_c";
	done;
	if [ -n "${_ret:-}" ]; then
		echo "${_ret}";
		return 0;
	fi;
	return 1;
}

Leave a Reply

Your email address will not be published. Required fields are marked *