Skip to content

String manipulation module

Wang Renxin edited this page Jul 4, 2016 · 3 revisions

A good part of BASIC is the string manipulation facilities. MY-BASIC already supplied some fundamental string functions, with which it's possible to implement more. This is a demonstration about how to implement a string manipulation module with MY-BASIC.

The LCASE and UCASE pair in MY-BASIC:

def lcase(c)
	s = ""
	for i = 0 to len(c) - 1
		d = mid(c, i, 1)
		if d >= "A" and d <= "Z" then
			s = s + chr(asc(d) + (asc("a") - asc("A")))
		else
			s = s + d
		endif
	next

	return s
enddef

def ucase(c)
	s = ""
	for i = 0 to len(c) - 1
		d = mid(c, i, 1)
		if d >= "a" and d <= "z" then
			s = s + chr(asc(d) + (asc("A") - asc("a")))
		else
			s = s + d
		endif
	next

	return s
enddef

And the usage:

print lcase("Hello");
print ucase("Hello");
Clone this wiki locally