Tokenize a string: Difference between revisions

Content added Content deleted
No edit summary
Line 2,326:
{{out}}
<pre>"Hello.How.Are.You.Today"</pre>
 
=={{header|QB64}}==
''CBTJD'': 2020/03012
<lang vb>a$ = "Hello,How,Are,You,Today" ' | Initialize original string.
FOR na = 1 TO LEN(a$) ' | Start loop to count number of commas.
IF MID$(a$, na, 1) = "," THEN nc = nc + 1 ' | For each comma, increment nc.
NEXT ' | End of loop.
DIM t$(nc) ' | Dim t$ array with total number of commas (nc). Array base is 0.
FOR nb = 1 TO LEN(a$) ' | Start loop to find each word.
c$ = MID$(a$, nb, 1) ' | Look at each character in the string.
IF c$ = "," THEN ' | If the character is a comma, increase the t$ array for the next word.
t = t + 1 ' | t = token word count. Starts at 0 because array base is 0.
ELSE ' | Or...
t$(t) = t$(t) + c$ ' | Add each characetr to the current token (t$) word.
END IF ' | End of decision tree.
NEXT ' | End of loop.
FOR nd = 0 TO t ' | Start loop to create final desired output.
tf$ = tf$ + t$(nd) + "." ' | Add each token word from t$ followed by a period to the final tf$.
NEXT ' | End of loop.
PRINT LEFT$(tf$, LEN(tf$) - 1) ' | Print all but the last period of tf$.
END ' | Program end.
</lang>
 
=={{header|R}}==