# Code Golf: Create a string of 100 of the same character [closed]

The problem is to create a string of the same character repeated 100 times.

One potential solution in JavaScript is:

var i = 100,
s = '';

while(i--) s += "Z";


Shortest code wins.

• This site is intended for puzzles only, not for practical problems. This creates three issues: 1. The task is too trivial to be interesting as a puzzle. 2. The really short answers aren't in JavaScript, so they won't help you. 3. In code golf, O(n) algorithms will have O(n!) implementations, undefined behavior will be invoked and pretty much anything else that should never, ever be present in code that is actually going to be used will be done, if it saves just a single byte. – Dennis May 16 '14 at 18:02
• I edited the question to be on topic but still allow for the existing answers. – mbomb007 Apr 6 '16 at 17:23
• Some answers took “create a string” literally and only generate the value leaving it lost in the RAM, while other answers stick with the default of outputting/returning the result. For trivial tasks like this, outputting/returning the result in some languages doubles the code length. I am not sure if this question is ready to be reopened. – manatwork Apr 6 '16 at 18:24
• 3-byte Jelly answer: ⁶×³. This is surely too trivial. – Erik the Outgolfer Sep 25 '16 at 18:10

## golflua: 109 15

w(S.t("a",100))


Which is more-or-less equivalent to Heiko's Lua answer

# Python2.7 - 7 bytes

'a'*100


String manipulation using * pretty simple.

# Lua, 24 bytes

This variant is based on Geobits' comment:

print(("").rep("a",100))


## Older version, 38 bytes

a=""for i=0,99 do a=a.."a"end print(a)


Ungolfed:

a = ""              -- start with empty string
for i = 0, 99, 1 do -- 100 times
a = a .. "a"      -- append "a" to variable a
end
print(a)            -- print result

• print(string.rep("a",100)) ? – Geobits May 16 '14 at 20:04
• @Geobits: Very good, much shorter (26 bytes). – Heiko Oberdiek May 16 '14 at 20:18
• print(("a"):rep(100)), use the object implementation of string.rep, 21 Bytes ;) – Katenkyo Apr 4 '16 at 13:57

# GolfScript, 4 characters:

100*


The input should be on the stack. This program assumes a single-character input.

If the character needs to be hardcoded, it's 7 characters:

'a'100*


If the character is a newline and it needs to be hardcoded, 5 characters (thanks to Jan Dvorak):

100n*

• Umm... are you expecting a single-character input? Because 100 times nothing is still nothing. – John Dvorak May 16 '14 at 16:39
• @JanDvorak: Yes, I am. The OP gave an example where he appended a single character 100 times, so I assumed that the input will always be a single character. – ProgramFOX May 16 '14 at 16:41
• In his case, the character was hard-coded in the statement. I that as that you are required to hard-code your own as well. – John Dvorak May 16 '14 at 16:42
• @JanDvorak: Ah, ok. I added an answer where the character is hardcoded, but I'll still keep the not-hardcoded part around if hardcoding the char is not necessary. – ProgramFOX May 16 '14 at 16:45
• also, n25* is an interesting solution ;-) – John Dvorak May 16 '14 at 16:52

PRINT STRING$(100,"A")  Why not? # JavaScript - 20 chars At least, shorter than Bash ;) Array(101).join("a")  An array of length 100 joined with letter a. # Perl - 12 chars First Perl golf ! print"a"x100  Thanks to ace for shortening one character. • Array(100).join(a) - 18 characters. An array of length 100 joined with a letter from variable a! :D – avall May 16 '14 at 17:10 • Is this a Ninja edit or did I not notice your Perl answer before I posted mine? And P.S. you can remove the space between print and "a"x100 – user12205 May 16 '14 at 17:12 • The Javascript solution should be Array(101). Array(100).join("a").length == 99 – nderscore May 16 '14 at 20:33 • @nderscore Thanks, fixing. – Gaurang Tandon May 17 '14 at 5:04 • @ace I had just quickly applied my new Perl skills to my post, and noticed that we had posted the same answer at the same time. I was about to notify you, but the electricity went out. And thanks for the tip :) – Gaurang Tandon May 17 '14 at 5:06 # J (7) Very similar to the Python one 100$'a'


ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ$ # Pure Bash, 31 bytes $ printf -vs %100s;echo ${s// /Z} ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ$


Pretty standard stuff.

# GNU dc, 14 bytes

2 800^255/90*P


Output:

$dc -e '2 800^255/90*P' ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ$


## Perl, 7 bytes

"a"x100


Almost completely the same as the Python one.

# Brainfuck, 27 characters (or 26 25)

,>++++++++++[<..........>-]


This program requires input, because hardcoding a character is not possible in Brainfuck.

If it is allowed to always output 100 times the NUL character, there is a 25 char solution (thanks to Quincunx):

++++++++++[>..........<-]

• Well actually it is possible... if you remove , you will be outputting 100 times of the NUL character. – user12205 May 16 '14 at 17:18
• @ace: Correct, but then you cannot specify the character. – ProgramFOX May 16 '14 at 17:18
• The question never said the character has to be specified anyway... – user12205 May 16 '14 at 17:19
• This is how I interpret it: "...to create a string of the same character repeated 100 times in as short a statement as possible. One potential solution is:" If you interpret it as having to output the Z characters for 100 times then I think it is still possible in BF: you need to add 90 + in front. – user12205 May 16 '14 at 17:23
• Actually, if you could output the NUL character, you could simply do ++++++++++[>..........<-], saving 2 chars from your 27 – Justin May 16 '14 at 20:51

replicate 100 'a'


GHCi output:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"


Code for GHC:

main = print \$ replicate 100 'a'

• No need for the space between 100 and 'a'`. – nimi Apr 6 '16 at 17:34