# Pointers, pointers, pointers!

## Introduction

In the error outputs of some languages such as Java, a pointer is shown to give the programmer an idea of exactly where the error went wrong.

Take this example on Ideone:

Main.java:12: error: ';' expected
Invalid Java!
^


Notice the caret shows where the invalid code is?

## Challenge

Your challenge is: given number N and string S, place a pointer on the Nth character in S.

## Examples

Input: 2, "Lorem ipsum, dollar sit amet."

Output:

Lorem ipsum, dollar sit amet.
^


## Rules

• Input is received via STDIN or function parameters
• Output is printed out to the console or returned
• Trailing new lines, spaces etc are allowed in the output
• The pointer character must be a ^ caret and must be on a new line.
• This is code golf, so the shortest answer wins. Good luck!
• I feel like this is an extremely simple problem, so I am not certain it will be received overly well. You might want to try the sandbox once you have enough rep. – FryAmTheEggman Jun 10 '15 at 1:08
• I think this could have been made a bit more interesting if the input had multiple lines, so that you had to insert a newline, spaces, and carat at the correct position(s). Honestly, the spec doesn't really say it will be a single line, but I think enforcing that now will invalidate a few answers unfairly, since there's no example that shows this. – Geobits Jun 10 '15 at 2:04
• Like I said, the cat's probably out of the bag on this one. Rule changes after valid answers are posted usually don't work out well. Live and learn ;) – Geobits Jun 10 '15 at 3:06
• @SohamChowdhury I'll just pretend that was intentional ;) Do you want me to fix it? – Matt Young Jun 10 '15 at 9:36
• While this may be very simple, you've certainly done well for a first challenge! You have +16/-0 votes, 1,300 views, and 28 answers (as of this writing) and you've made the Hot Network Questions list. Nice job! – Alex A. Jun 10 '15 at 14:50

# Perl, 45

This is a pretty horribly golfed answer, but it's my first attempt at code golf.

<>=~/(.*), "(.*)"/;print"$2 "." "x($1-1)."^";

• Welcome to PPCG! If you enjoy Perl, reading Tips for golfing in Perl? is a must. Using the -p switch (counted as 1 byte), you can eliminate <>=~ and replace print with $_=. If you take a few liberties with the input format on top of that, you can shorten your code to /\d+ /;$_=$'.$"x($&-1)."^". – Dennis Jun 11 '15 at 5:57 ### Ruby, 1723 22 ->s,n{s+$/+?\s*~-n+?^}


This is as short as I can go from something like this:

# Lambdas are like Procs, except with a fixed num of arguments
lambda do |sourceLine, offset|
# The last expression is returned as the lambda's return value.
# Multiplying a string by N will repeat it N times.
sourceLine + "\n" + " " * (offset - 1) + "^"
end

• And where are you outputting/returning the source code line to which the caret points? – manatwork Jun 10 '15 at 18:28
• The rules say: "Output is printed out to the console or returned". I am returning the output. – boxmein Jun 10 '15 at 18:32
• The accent was not on the action but on the subject: your proc neither receives nor returns the source code. – manatwork Jun 10 '15 at 18:35
• Oh, sorry! Edited the code. – boxmein Jun 10 '15 at 18:51
• You can save 1 char by replacing ?\n with $/. – Tony Ellis Jun 11 '15 at 8:39 # Pascal: 57 characters procedure p(n:Byte;s:string);begin Write(s,#10,'^':n)end;  Just to demonstrate Pascal's cute little output formatting. The rest is depressing. # Perl, 20 After seeing @Digital Trauma's answer, I wanted to port his answer to Perl to show an another example of accepting the input numbers as unary as a default. This is based on the meta discussion. $i++&y/1/ /&s/ $/^/  19 characters +1 for -p flag. Run with: { echo "Lorem ipsum, dollar sit amet."; echo 111; } | perl -pe'$i++?y/1/ /&&s/ $/^/:1'  • The ternary operator seems pointless here as you only have instruction for the “then” branch: $i++&y/1/ /&s/ $/^/. – manatwork Jun 12 '15 at 15:17 • @manatwork good call! updated answer – hmatt1 Jun 12 '15 at 15:19 # C, 60 49 f(n,s){puts(s);while(n-=1)putch(' ');putch('^');}  First post, so I guess it's not that good. # Scala (49 42 bytes) def p(i:Int,s:String)=s+"\n"+" "*(i-1)+"^"  scala> def p(i:Int,s:String)=s+"\n"+" "*(i-1)+"^" p: (i: Int, s: String)String scala> p(2, "Lorem ipsum, dollar sit amet.") res0: String = Lorem ipsum, dollar sit amet. ^  # J, 19 Including parentheses! The verb: (],:'^',~' '#~<:@[)  Use: 9 (],:'^',~' '#~<:@[) 'Invalid Java!' Invalid Java! ^  This is my first code golf, but here I go. # Haskell, 36 f s n=s++'\n':replicate(n-1)' '++"^"  The regular version being placeCaret :: String -> Integer -> String placeCaret s n = s ++ '\n' : replicate (n - 1) ' ' ++ "^"  So, not really much change. Clearly, the replicate is wasting the most space here, but as far as I know there are no alternatives in Prelude. I played a bit with other things which could have been cool tricks, but they all ended up being the same length. Example usage: Prelude> putStrLn$ f ['a'..'z'] 7
abcdefghijklmnopqrstuvwxyz
^


putStrLn is needed so that the newline will be displayed as a newline instead of "\n" (in GHCi only).

I know I'm VERY late to this party, but whatever.

## Straw, 13 bytes

<#<>
> ,}*>^>


Try it online!

^ is the only non-alphanumeric character (with \) to not be assigned to a command, so no need for a string literal to push it.

# Haskell, 30 bytes

s!n=s++'\n':([1..n]>>" ")++"^"


# C++ (with the standard library) - 159 121 bytes (-38 by Riley)

#include<iostream>
using namespace std;int main(int c,char**v){cout<<string(v[2])<<endl<<string(stoi(v[1])-1,' ')<<"^";}


Gets input from the command line args.
Example usage: ./a.out 8 "hello, world"

This is my first ever code golf, so if you have any tips, that would be great!

• 1. Unless specified, you can assume the input will be correctly formatted. That lets you skip the exit part. 2. You shouldn't need to assign p and s. I think you can just put stoi(...) and string(...) right in the cout. 3. I don't think you need the space in char** v. I hope this helps :) – Riley Feb 2 '17 at 14:52
• @Riley Thanks! That helped. – Admicos Feb 2 '17 at 15:00
• You shouldn't need the space in stoi either. – Riley Feb 2 '17 at 15:04

## Common Lisp, 40 35

(format t"~A


-5 bytes thanks to PrzemysławP.

• (format t"~A<enter>~VT^"(read)(1-(read))) saves 5 bytes – user65167 May 13 '17 at 17:57
• @PrzemysławP Thanks, I edited the answer; I was pretty busy, sorry for the delay – coredump Jun 7 '17 at 8:19

# rs, 35 bytes

(\d+), "(.*?)"/\2\n( )^^(\1)^
\^/^


Unfortunately, the online demo has a bug that I need to fix with newlines, so I can't put a demo link.

EDIT: I fixed the bug in the demo page. So...

Live demo!

This is pretty simple:

• Line 1 places the text, a newline, and a series of spaces repeated one more times than we want.
• Line 2 gets rid of the extra space.

# Lua, 58 53 bytes

a=" "function p(n,s)print(s,"\n"..a:rep(n-1).."^")end


Thanks to manatwork this just got shorter by 5 bytes. Looks like I need to refresh my Lua skills.

First version:

function p(n,s)print(s.."\n"..string.rep(" ",n-1).."^")end

• That static method call is horribly long. function p(n,s)x=" ";print(s.."\n"..x:rep(n-1).."^")end – manatwork Jun 10 '15 at 17:24
• Actually you can remove one more character. As “Trailing new lines, spaces etc are allowed in the output”, replace the first .. with ,. This way a trailing tab will appear in the first line, but is allowed. – manatwork Jun 10 '15 at 18:37

# PHP 4.1, 35 31 bytes

This one was really simple, but still fun!

<?="$S ",str_pad('^',$P,' ',0);


You can pass the values over POST, GET, SESSIONS, ...

The key S must contain the string, while the key P must contain the character number where you want the ^.

Old answer (35 bytes):

<?="$S ",str_repeat(' ',--$P),'^';


## F#, 31 characters

let p n s=s+"\n"+"^".PadLeft(n)


# Batch, 56 Bytes

@set/p=
@for /l %%a in (2,1,%1)do @cd|set/p=.<BS>
@echo ^^


This code contains two points of interest. First, it takes input from both StdIn and command line arguments. The string is read from StdIn while the index of the caret is taken as a parameter. Second, Batch doesn't play nice when it come to outputting a single space character (U+0020) so a bit of a work around is required: cd|set/p=.<BS> In this line <BS> stands in for the backspace character (U+0008) and the last character is a space. This is accounted for in the byte count.

# SAS, 22

macro a put &s/@&n'^'%


Usage:

%let s="Lorem ipsum dolor sit amet";
%let n=2;
data;a;run;


Output:

Lorem ipsum dolor sit amet
^


If running in 'batch' mode, the run is not required so the code in needed to produce the function and run it (just not setting the inputs) would be 30 bytes

macro a put &s/@&n'^'% data;a;


So, it may be cheating... this style macro(function) definition in SAS does not have parameters, so the inputs are defined separately, in Usage as two %let statements. Parsing from STDIN would substantially lengthen the solution and only works when running SAS in 'batch' mode, which is not necessarily commonplace.

# Javascript, 50 bytes

Code

function f(n,s){return s+'\n'+s.substr(0,n-1)+'^'}

Output

console.log(f(2, "hello"));
hello
h^


This approach takes advantage of the fact that you do not specify an empty string preceding the caret, only a caret pointing to the index in the input string and anything following that. If you happen to have a caret in your input string and an error there then it might get a little bit confusing :)

For example

console.log(f(3, "he^lo"));
he^lo
he^


or

console.log(f(3, "he^"));
he^
he^


# Turtlèd, 17 bytes (non-competing)

r!-[*.-l_]' d?:'^


## explanation

r!-               move right, take input into string variable, decrement string pointer
[*    ]        while current char is not *
.-           write pointed char from string, decrement pointer
l_         move left, write * if the pointer is on the last char of string
' d     write space, move down
?:'^ Take input into register, move forward by the amount in register, write ^


# Pyth, 8 bytes

z+*dtQ\^


Takes inputs separated by a newline (i.e. 2\nLorem ipsum).
Try it online!

Explanation:

z           The second input, implicitly printed with trailing newline
*d__      The space character, __ times
tQ        where __ is the first input -1
+    \^    Append a literal carat character and printed


# Jelly, 8 7 bytes (non-competing)

Ṅ⁶ṁ”^⁹¦


Try it online!

That dollardolor makes me laugh!!

Thanks to @Dennis for -1.

# Vim, 7 keystrokes (buffer input/output, excluding input keystrokes)

o<Esc> + input number + i <Esc>r^

## Explanation

• o<Esc> creates a new line
• #i <Esc> creates # spaces
• r^ replaces the last space by ^

Note: This answers does not write the output to stdout (:w !tee, + 6 keystrokes) - so maybe it will be considered invalid.

# C++14, 45 bytes

As unnamed lambda modifying its input, assuming s is similar to std::string (has operator+=):

[](int n,auto&s){for(s+=10;--n;s+=32);s+=94;}


Usage:

#include<string>
#include<iostream>

auto f =
[](int n,auto&s){for(s+=10;--n;s+=32);s+=94;}
;

int main() {
std::string s = "abcdefghijk";
f(2,s);
std::cout << s << std::endl;
}


# 05AB1E, 7 bytes (non-competing)

'^I<ú‚»


Try it online!

Explantion

'^       # push caret
I<ú    # prepend with input-1 spaces
‚   # pair with second input
»  # join by newline


# SmileBASIC, 26 bytes

INPUT N,S$?S$?" "*(N-1);"^


1-indexed arrow adds 4 bytes...

• Note that you only want N-1 spaces before the caret. – Nick Matteo Feb 2 '17 at 23:08

# Gol><> (Golfish), 14 bytes (non-competing)

^IMR aTiEHot


Explanation

^             # push caret
I            # read input as number
M           # subtract 1
R         # pop N, and Repeatedly push a space, N times
a       # push 10 (ASCII newline)
T      # set teleport pad
i     # input character
EH   # if EOF, output stack and halt
o  # otherwise, output the character just read
t # teleport back to pad


# tcl, 47

gets stdin s
puts \$s\n[format %[gets stdin]s ^]


demo

# Vim 9 bytes

jD@"a <esc>r^


Try it online!

Note that <esc> is a single byte, namely 0x1B, but since that byte is unprintable, I added a flag and used <esc> instead. This version uses the unprintable.

Explanation:

j               " Move down one line
D              " Delete everything on this line, and save it in the unnamed register
@"            " 'n' times
a <esc>     " Append a 'space'
r^   " Replace this last space with caret


# V, 6 bytes (noncompeting)

ïÀá r^


Try it online!