Count occurrences of a character in a string
Posted: Tue Jan 03, 2017 7:31 pm
I just count them
Mr.Kibernetik software support
https://nitisara.ru/forum/
Code: Select all
'option base 1
a$="Now is the time for all good men to come to the aid of their country"
i$="i"
PRINT "String: "&a$
PRINT "Character: "&i$
PRINT
chk=count_instr(a$,i$)
PRINT "Character '"&i$&"' appears "&chk&" times in string."
a$="Now is the time for all good men to come to the aid of their country"
i$="he"
PRINT "String: "&a$
PRINT "Character: "&i$
PRINT
chk=count_instr(a$,i$)
PRINT "Character '"&i$&"' appears "&chk&" times in string."
a$="xxxx"
i$="xx"
PRINT "String: "&a$
PRINT "Character: "&i$
PRINT
chk=count_instr(a$,i$)
PRINT "Character '"&i$&"' appears "&chk&" times in string."
print "option_base(): ";option_base()
DEF count_instr(string$,SUB$)
result=INSTR(string$,SUB$)
IF result=-1 THEN RETURN 0
IF result=LEN(string$)-LEN(SUB$)+OPTION_BASE() THEN RETURN 1
RETURN 1+ count_instr(MID$(string$,result+LEN(SUB$)),SUB$)
END DEF