Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ challenge: random BBCode

Name: Anonymous 2018-07-23 23:47

Write a program that iterates through a string or char array input and adds random BBCode. There are 9 tags to account for: [b], [i], [o], [u], [s], [spoiler], [code], [sup], and [sub].

Each character can have multiple BBCode tags. So you could roll a random number to determine if you want to add it to that particular character or not. I'll let you decide on the percentages for randomness. It's doesn't necessarily have to be 50%.

You could either have them all open and close for every char, or perhaps just keep track of which tags are opened, and then make sure they're all closed before the end. But the only ones that would really be interesting for multiple characters instead of just one would be code, sub, sup, and spoiler.

I made a very basic example here, but you should try to add more features:


import java.lang.Math.*;

public class BBCode {
public static void main(String[] args) {
String inputString = "here is an example";
String outputString = "";
for (Integer i = 0; i < inputString.length(); i++) {
int bold = (int) ( Math.random() * 2 + 1);
int italic = (int) ( Math.random() * 2 + 1);
int overline = (int) ( Math.random() * 2 + 1);
int underline = (int) ( Math.random() * 2 + 1);
String openingTags = "";
String closingTags = "";
//need to do openingTags in order, then closingTags in reverse order
//example: [b][i][o][u]hello[/u][/o][/i][/b]
//NOT: [b][i][o][u]hello[/b][/i][/o][/u] (that's wrong)
if (bold == 1) {
openingTags += "[b]";
}
if (italic == 1) {
openingTags += "[i]";
}
if (overline == 1) {
openingTags += "[o]";
}
if (underline == 1) {
openingTags += "[u]";
}

if (underline == 1) {
closingTags += "[/u]";
}
if (overline == 1) {
closingTags += "[/o]";
}
if (italic == 1) {
closingTags += "[/i]";
}
if (bold == 1) {
closingTags += "[/b]";
}
outputString += openingTags + inputString.charAt(i) + closingTags;

}
System.out.print(outputString);
}
}


Example output of above code:
here is an example

Name: Anonymous 2018-08-19 21:42

Disclaimer:
RNG Subroutine stolen from GNU docs.

PROGRAM BBCODE
IMPLICIT NONE

CHARACTER(LEN=50) :: STRING
CHARACTER(LEN=7),
& DIMENSION(9) :: BBCODES
INTEGER :: POS, I
REAL :: T

STRING="Ok, you just fuqin angered an expert programmer"
POS=1
BBCODES=["b ","i ","o ","u ","s ",
& "spoiler","code ","sup ","sub "]

CALL INIT_RANDOM_SEED()
1 CALL RANDOM_NUMBER(T)
I=INT(T*10)
IF(I==0) GO TO 1

WRITE(*,FMT="(A,A)",ADVANCE="NO") "["//TRIM(BBCODES(I))//"]",
& STRING(POS:POS)//"[/"//TRIM(BBCODES(I))//"]"

POS=POS+1
IF(POS<LEN(STRING)) GO TO 1

END PROGRAM BBCODE


SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed

CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))

CALL SYSTEM_CLOCK(COUNT=clock)

seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)

DEALLOCATE(seed)
END SUBROUTINE


Output:
Ok, you just fuqin angered an expert programmer

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List