50
\$\begingroup\$

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!
\$\endgroup\$
  • 16
    \$\begingroup\$ 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. \$\endgroup\$ – FryAmTheEggman Jun 10 '15 at 1:08
  • 8
    \$\begingroup\$ 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. \$\endgroup\$ – Geobits Jun 10 '15 at 2:04
  • 3
    \$\begingroup\$ 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 ;) \$\endgroup\$ – Geobits Jun 10 '15 at 3:06
  • 1
    \$\begingroup\$ @SohamChowdhury I'll just pretend that was intentional ;) Do you want me to fix it? \$\endgroup\$ – Matt Y Jun 10 '15 at 9:36
  • 10
    \$\begingroup\$ 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! \$\endgroup\$ – Alex A. Jun 10 '15 at 14:50

71 Answers 71

1 2
3
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Jelly, 8 7 bytes (non-competing)

Ṅ⁶ṁ”^⁹¦

Try it online!

That dollardolor makes me laugh!!

Thanks to @Dennis for -1.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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;
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 26 bytes

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

1-indexed arrow adds 4 bytes...

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Note that you only want N-1 spaces before the caret. \$\endgroup\$ – Nick Matteo Feb 2 '17 at 23:08
0
\$\begingroup\$

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

`^IMR` aTiEHot

Try it here.

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

tcl, 47

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

demo

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

V, 6 bytes (noncompeting)

ïÀá r^

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Braingolf, 25 bytes [non-competing]

VV"^ "R3-M#
&@v[v.Rv]v<&@

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby 2.7, 24 bytes

x=->{_1+'
'+" "*~-_2+?^}
| improve this answer | |
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.