Program for converting a boring text into a stylish written text
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
I have written a program that makes it possible to write as cool as the cool kids. All you have to do is enter the boring, normal text, which is then transformed into an exciting, stylish text.
Is there a possibilty to make the code more performant without decreasing it's readability?
Example Output:
I H@Ve wRITTen @ Pr0qR@M tH@T m@KEZ IT P0ZzIblE t0 wriTE @z C00L @z the c00L KIdZ :) All Y0U h@Ve T0 D0 iZ eNter thE B0rinQ, n0rm@L teXt, whIch iZ tHeN TR@nZF0RmED int0 @n exCiTInq, ztyLIZh TExT. :D
Source code
import java.util.Random;
import java.util.Scanner;
public class Program
public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String text = input.nextLine();
String trendyText = convertToTrendyText(text);
System.out.println(trendyText);
public static String convertToTrendyText(String string)
string = string.replace("g", "q");
string = string.replace("s", "z");
string = string.replace("a", "@");
string = string.replace("o", "0");
string = string.replace(". ", " " + generateRandomSmiley() + " ");
string = string.replace("! ", " " + generateRandomSmiley() + " ");
string = string.replace("? ", " " + generateRandomSmiley() + " ");
string = string.concat(" " + generateRandomSmiley());
StringBuilder text = new StringBuilder(string);
Random random = new Random();
for (int i = 0; i < text.length(); i++)
if (random.nextBoolean())
text.setCharAt(i, Character.toUpperCase(text.charAt(i)));
return text.toString();
public static String generateRandomSmiley()
Random random = new Random();
switch (random.nextInt(10))
case 0: return ":)";
case 1: return ":D";
case 2: return ":*";
case 3: return "<3";
case 4: return "o.O";
case 5: return "x3";
case 7: return "xD";
case 8: return ":o";
default: return ";D";
java
add a comment |Â
up vote
4
down vote
favorite
I have written a program that makes it possible to write as cool as the cool kids. All you have to do is enter the boring, normal text, which is then transformed into an exciting, stylish text.
Is there a possibilty to make the code more performant without decreasing it's readability?
Example Output:
I H@Ve wRITTen @ Pr0qR@M tH@T m@KEZ IT P0ZzIblE t0 wriTE @z C00L @z the c00L KIdZ :) All Y0U h@Ve T0 D0 iZ eNter thE B0rinQ, n0rm@L teXt, whIch iZ tHeN TR@nZF0RmED int0 @n exCiTInq, ztyLIZh TExT. :D
Source code
import java.util.Random;
import java.util.Scanner;
public class Program
public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String text = input.nextLine();
String trendyText = convertToTrendyText(text);
System.out.println(trendyText);
public static String convertToTrendyText(String string)
string = string.replace("g", "q");
string = string.replace("s", "z");
string = string.replace("a", "@");
string = string.replace("o", "0");
string = string.replace(". ", " " + generateRandomSmiley() + " ");
string = string.replace("! ", " " + generateRandomSmiley() + " ");
string = string.replace("? ", " " + generateRandomSmiley() + " ");
string = string.concat(" " + generateRandomSmiley());
StringBuilder text = new StringBuilder(string);
Random random = new Random();
for (int i = 0; i < text.length(); i++)
if (random.nextBoolean())
text.setCharAt(i, Character.toUpperCase(text.charAt(i)));
return text.toString();
public static String generateRandomSmiley()
Random random = new Random();
switch (random.nextInt(10))
case 0: return ":)";
case 1: return ":D";
case 2: return ":*";
case 3: return "<3";
case 4: return "o.O";
case 5: return "x3";
case 7: return "xD";
case 8: return ":o";
default: return ";D";
java
2
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have written a program that makes it possible to write as cool as the cool kids. All you have to do is enter the boring, normal text, which is then transformed into an exciting, stylish text.
Is there a possibilty to make the code more performant without decreasing it's readability?
Example Output:
I H@Ve wRITTen @ Pr0qR@M tH@T m@KEZ IT P0ZzIblE t0 wriTE @z C00L @z the c00L KIdZ :) All Y0U h@Ve T0 D0 iZ eNter thE B0rinQ, n0rm@L teXt, whIch iZ tHeN TR@nZF0RmED int0 @n exCiTInq, ztyLIZh TExT. :D
Source code
import java.util.Random;
import java.util.Scanner;
public class Program
public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String text = input.nextLine();
String trendyText = convertToTrendyText(text);
System.out.println(trendyText);
public static String convertToTrendyText(String string)
string = string.replace("g", "q");
string = string.replace("s", "z");
string = string.replace("a", "@");
string = string.replace("o", "0");
string = string.replace(". ", " " + generateRandomSmiley() + " ");
string = string.replace("! ", " " + generateRandomSmiley() + " ");
string = string.replace("? ", " " + generateRandomSmiley() + " ");
string = string.concat(" " + generateRandomSmiley());
StringBuilder text = new StringBuilder(string);
Random random = new Random();
for (int i = 0; i < text.length(); i++)
if (random.nextBoolean())
text.setCharAt(i, Character.toUpperCase(text.charAt(i)));
return text.toString();
public static String generateRandomSmiley()
Random random = new Random();
switch (random.nextInt(10))
case 0: return ":)";
case 1: return ":D";
case 2: return ":*";
case 3: return "<3";
case 4: return "o.O";
case 5: return "x3";
case 7: return "xD";
case 8: return ":o";
default: return ";D";
java
I have written a program that makes it possible to write as cool as the cool kids. All you have to do is enter the boring, normal text, which is then transformed into an exciting, stylish text.
Is there a possibilty to make the code more performant without decreasing it's readability?
Example Output:
I H@Ve wRITTen @ Pr0qR@M tH@T m@KEZ IT P0ZzIblE t0 wriTE @z C00L @z the c00L KIdZ :) All Y0U h@Ve T0 D0 iZ eNter thE B0rinQ, n0rm@L teXt, whIch iZ tHeN TR@nZF0RmED int0 @n exCiTInq, ztyLIZh TExT. :D
Source code
import java.util.Random;
import java.util.Scanner;
public class Program
public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String text = input.nextLine();
String trendyText = convertToTrendyText(text);
System.out.println(trendyText);
public static String convertToTrendyText(String string)
string = string.replace("g", "q");
string = string.replace("s", "z");
string = string.replace("a", "@");
string = string.replace("o", "0");
string = string.replace(". ", " " + generateRandomSmiley() + " ");
string = string.replace("! ", " " + generateRandomSmiley() + " ");
string = string.replace("? ", " " + generateRandomSmiley() + " ");
string = string.concat(" " + generateRandomSmiley());
StringBuilder text = new StringBuilder(string);
Random random = new Random();
for (int i = 0; i < text.length(); i++)
if (random.nextBoolean())
text.setCharAt(i, Character.toUpperCase(text.charAt(i)));
return text.toString();
public static String generateRandomSmiley()
Random random = new Random();
switch (random.nextInt(10))
case 0: return ":)";
case 1: return ":D";
case 2: return ":*";
case 3: return "<3";
case 4: return "o.O";
case 5: return "x3";
case 7: return "xD";
case 8: return ":o";
default: return ";D";
java
edited Apr 30 at 15:56
asked Apr 30 at 15:43
Dexter Thorn
657521
657521
2
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56
add a comment |Â
2
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56
2
2
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Finally. A program to help me speak the same language as the cool kids. Thanks for that.
Use an array when it's good enough
The second part of convertToTrendyText
needlessly uses a StringBuilder
instance.
The power of StringBuilder
is to efficiently build strings whose size is not known in advance.
In this method we do know in advance,
so there's no need for a StringBuilder
,
an array from string.toCharArray()
would be more than enough.
After replacing characters in the char
,
you could return it in a new String(...)
.
Destructive uppercasing
The first part of convertToTrendyText
inserts some random smileys.
The second part of the method randomly uppercases some letters.
That risks ruining the following smileys: o.O
, x3
, xD
.
I'm wondering if that's intended or not.
With this potential unintended side effect,
the text might become completely unreadable.
To avoid such destruction,
you could swap the first and second parts:
do the uppercasing first,
and insert smileys after.
That way the smileys will be unaffected by design.
One Random
is enough
There's no need to create multiple instances of Random
in a program.
It would be better to use just one.
That could be a good step in the direction of making the program testable,
because you will be able to set a seed to get reproducible output.
Getting a random value out of n
values
The switch
statement in generateRandomSmiley
is a bit troublesome.
If you add a new smiley, you have to remember to increment the number in the random.nextInt(...)
call, and add a correctly numbered case
statement. Such a hassle.
If you use an array of smileys,
then the process of adding or removing values becomes a lot simpler,
more compact,
without having to worry about indexes.
private static final String SMILEYS =
":)", ":D", ":*", "<3", "o.O", "x3", ";D", "xD", ":o", ";D"
;
public static String generateRandomSmiley()
return SMILEYS[random.nextInt(SMILEYS.length)];
Btw, did you notice that there is no case 6
line in your original switch
statement?
That leads to getting the same value for it as the default
case.
Not sure if that was intentional.
To preserve the behavior of the posted code,
I duplicated the default value ;D
at index 6
(in addition to its natural index 9
).
A word on performance
Every call like string = string.replace("...", "...");
has to iterate over all the content of the string.
That seems a bit wasteful.
In a toy program like this,
it doesn't really matter,
but it's worth keeping in mind.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Finally. A program to help me speak the same language as the cool kids. Thanks for that.
Use an array when it's good enough
The second part of convertToTrendyText
needlessly uses a StringBuilder
instance.
The power of StringBuilder
is to efficiently build strings whose size is not known in advance.
In this method we do know in advance,
so there's no need for a StringBuilder
,
an array from string.toCharArray()
would be more than enough.
After replacing characters in the char
,
you could return it in a new String(...)
.
Destructive uppercasing
The first part of convertToTrendyText
inserts some random smileys.
The second part of the method randomly uppercases some letters.
That risks ruining the following smileys: o.O
, x3
, xD
.
I'm wondering if that's intended or not.
With this potential unintended side effect,
the text might become completely unreadable.
To avoid such destruction,
you could swap the first and second parts:
do the uppercasing first,
and insert smileys after.
That way the smileys will be unaffected by design.
One Random
is enough
There's no need to create multiple instances of Random
in a program.
It would be better to use just one.
That could be a good step in the direction of making the program testable,
because you will be able to set a seed to get reproducible output.
Getting a random value out of n
values
The switch
statement in generateRandomSmiley
is a bit troublesome.
If you add a new smiley, you have to remember to increment the number in the random.nextInt(...)
call, and add a correctly numbered case
statement. Such a hassle.
If you use an array of smileys,
then the process of adding or removing values becomes a lot simpler,
more compact,
without having to worry about indexes.
private static final String SMILEYS =
":)", ":D", ":*", "<3", "o.O", "x3", ";D", "xD", ":o", ";D"
;
public static String generateRandomSmiley()
return SMILEYS[random.nextInt(SMILEYS.length)];
Btw, did you notice that there is no case 6
line in your original switch
statement?
That leads to getting the same value for it as the default
case.
Not sure if that was intentional.
To preserve the behavior of the posted code,
I duplicated the default value ;D
at index 6
(in addition to its natural index 9
).
A word on performance
Every call like string = string.replace("...", "...");
has to iterate over all the content of the string.
That seems a bit wasteful.
In a toy program like this,
it doesn't really matter,
but it's worth keeping in mind.
add a comment |Â
up vote
3
down vote
accepted
Finally. A program to help me speak the same language as the cool kids. Thanks for that.
Use an array when it's good enough
The second part of convertToTrendyText
needlessly uses a StringBuilder
instance.
The power of StringBuilder
is to efficiently build strings whose size is not known in advance.
In this method we do know in advance,
so there's no need for a StringBuilder
,
an array from string.toCharArray()
would be more than enough.
After replacing characters in the char
,
you could return it in a new String(...)
.
Destructive uppercasing
The first part of convertToTrendyText
inserts some random smileys.
The second part of the method randomly uppercases some letters.
That risks ruining the following smileys: o.O
, x3
, xD
.
I'm wondering if that's intended or not.
With this potential unintended side effect,
the text might become completely unreadable.
To avoid such destruction,
you could swap the first and second parts:
do the uppercasing first,
and insert smileys after.
That way the smileys will be unaffected by design.
One Random
is enough
There's no need to create multiple instances of Random
in a program.
It would be better to use just one.
That could be a good step in the direction of making the program testable,
because you will be able to set a seed to get reproducible output.
Getting a random value out of n
values
The switch
statement in generateRandomSmiley
is a bit troublesome.
If you add a new smiley, you have to remember to increment the number in the random.nextInt(...)
call, and add a correctly numbered case
statement. Such a hassle.
If you use an array of smileys,
then the process of adding or removing values becomes a lot simpler,
more compact,
without having to worry about indexes.
private static final String SMILEYS =
":)", ":D", ":*", "<3", "o.O", "x3", ";D", "xD", ":o", ";D"
;
public static String generateRandomSmiley()
return SMILEYS[random.nextInt(SMILEYS.length)];
Btw, did you notice that there is no case 6
line in your original switch
statement?
That leads to getting the same value for it as the default
case.
Not sure if that was intentional.
To preserve the behavior of the posted code,
I duplicated the default value ;D
at index 6
(in addition to its natural index 9
).
A word on performance
Every call like string = string.replace("...", "...");
has to iterate over all the content of the string.
That seems a bit wasteful.
In a toy program like this,
it doesn't really matter,
but it's worth keeping in mind.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Finally. A program to help me speak the same language as the cool kids. Thanks for that.
Use an array when it's good enough
The second part of convertToTrendyText
needlessly uses a StringBuilder
instance.
The power of StringBuilder
is to efficiently build strings whose size is not known in advance.
In this method we do know in advance,
so there's no need for a StringBuilder
,
an array from string.toCharArray()
would be more than enough.
After replacing characters in the char
,
you could return it in a new String(...)
.
Destructive uppercasing
The first part of convertToTrendyText
inserts some random smileys.
The second part of the method randomly uppercases some letters.
That risks ruining the following smileys: o.O
, x3
, xD
.
I'm wondering if that's intended or not.
With this potential unintended side effect,
the text might become completely unreadable.
To avoid such destruction,
you could swap the first and second parts:
do the uppercasing first,
and insert smileys after.
That way the smileys will be unaffected by design.
One Random
is enough
There's no need to create multiple instances of Random
in a program.
It would be better to use just one.
That could be a good step in the direction of making the program testable,
because you will be able to set a seed to get reproducible output.
Getting a random value out of n
values
The switch
statement in generateRandomSmiley
is a bit troublesome.
If you add a new smiley, you have to remember to increment the number in the random.nextInt(...)
call, and add a correctly numbered case
statement. Such a hassle.
If you use an array of smileys,
then the process of adding or removing values becomes a lot simpler,
more compact,
without having to worry about indexes.
private static final String SMILEYS =
":)", ":D", ":*", "<3", "o.O", "x3", ";D", "xD", ":o", ";D"
;
public static String generateRandomSmiley()
return SMILEYS[random.nextInt(SMILEYS.length)];
Btw, did you notice that there is no case 6
line in your original switch
statement?
That leads to getting the same value for it as the default
case.
Not sure if that was intentional.
To preserve the behavior of the posted code,
I duplicated the default value ;D
at index 6
(in addition to its natural index 9
).
A word on performance
Every call like string = string.replace("...", "...");
has to iterate over all the content of the string.
That seems a bit wasteful.
In a toy program like this,
it doesn't really matter,
but it's worth keeping in mind.
Finally. A program to help me speak the same language as the cool kids. Thanks for that.
Use an array when it's good enough
The second part of convertToTrendyText
needlessly uses a StringBuilder
instance.
The power of StringBuilder
is to efficiently build strings whose size is not known in advance.
In this method we do know in advance,
so there's no need for a StringBuilder
,
an array from string.toCharArray()
would be more than enough.
After replacing characters in the char
,
you could return it in a new String(...)
.
Destructive uppercasing
The first part of convertToTrendyText
inserts some random smileys.
The second part of the method randomly uppercases some letters.
That risks ruining the following smileys: o.O
, x3
, xD
.
I'm wondering if that's intended or not.
With this potential unintended side effect,
the text might become completely unreadable.
To avoid such destruction,
you could swap the first and second parts:
do the uppercasing first,
and insert smileys after.
That way the smileys will be unaffected by design.
One Random
is enough
There's no need to create multiple instances of Random
in a program.
It would be better to use just one.
That could be a good step in the direction of making the program testable,
because you will be able to set a seed to get reproducible output.
Getting a random value out of n
values
The switch
statement in generateRandomSmiley
is a bit troublesome.
If you add a new smiley, you have to remember to increment the number in the random.nextInt(...)
call, and add a correctly numbered case
statement. Such a hassle.
If you use an array of smileys,
then the process of adding or removing values becomes a lot simpler,
more compact,
without having to worry about indexes.
private static final String SMILEYS =
":)", ":D", ":*", "<3", "o.O", "x3", ";D", "xD", ":o", ";D"
;
public static String generateRandomSmiley()
return SMILEYS[random.nextInt(SMILEYS.length)];
Btw, did you notice that there is no case 6
line in your original switch
statement?
That leads to getting the same value for it as the default
case.
Not sure if that was intentional.
To preserve the behavior of the posted code,
I duplicated the default value ;D
at index 6
(in addition to its natural index 9
).
A word on performance
Every call like string = string.replace("...", "...");
has to iterate over all the content of the string.
That seems a bit wasteful.
In a toy program like this,
it doesn't really matter,
but it's worth keeping in mind.
answered Apr 30 at 20:06
janos
95.5k12120343
95.5k12120343
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193276%2fprogram-for-converting-a-boring-text-into-a-stylish-written-text%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Cough The cool kids write in Times New Roman font size 12. JK
â FreezePhoenix
Apr 30 at 16:56