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-07-24 10:50

>>2
bruh I made that example code in like 2 minutes

the point of this thread is for other people to make their own version, not nitpick over what I did

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