I'm surprised that we haven't had a what's my middle name challenge on this site yet. I did alot of searching but found nothing. If this is a dup, please flag it as such.

Your challenge

Parse a string that looks like Jo Jean Smith and return Jean.

Test cases

Input: Samantha Vee Hills
Output: Vee

Input: Bob Dillinger
Output: (empty string or newline)

Input: John Jacob Jingleheimer Schmidt
Output: Jacob Jingleheimer

Input: Jose Mario Carasco-Williams
Output: Mario

Input: James Alfred Van Allen
Output: Alfred Van 

(That last one is incorrect technically, but fixing that would be too hard.)

Notes:

  • Names will always have at least 2 space-separated parts, with unlimited middle names between them or can be a list/array of strings.
  • Names may contain the alphabet (case-insensitive) and - (0x2d)
  • You may output a trailing newline.
  • You may require input to have a trailing newline.
  • Input from STDIN, a function parameter, or command-line argument is allowed, but hard-coding it in is not allowed.
  • Standard loopholes forbidden.
  • Output may be function return value, STDOUT, STDERR, etc.
  • Trailing spaces/newlines/tabs in the output are allowed.
  • Any questions? Comment below!

This is , so the shortest anwser in bytes wins!

share|improve this question
1  
Can input be taken in as list? – Anthony Pham 2 days ago
2  
Can the output be a list of strings? – Anthony Pham 2 days ago
5  
If other formats than a space-separated string are allowed, please edit that into the specification. – Martin Ender 2 days ago
5  
@programmer5000: if the input can be a list of strings, how about the output? Is ["John", "Jacob", "Jingleheimer", "Schmidt"] -> ["Jacob", "Jingleheimer"] a valid solution? – nimi 2 days ago
3  
Are leading spaces allowed? – betseg 2 days ago

44 Answers 44

Ohm, 2 bytes (CP437)

Accepts and returns a list of strings.

()

Explanation:

()   Main wire, arguments: a

(    Remove the first element of a
 )   ...and then the last element of that
     Implicit output
share|improve this answer
10  
The right tool for the job I guess – Rohan Jhunjhunwala 2 days ago

Vim, 6 5 bytes

dW$BD

Try it online!

(outputs with a trailing space)

Since Vim is reverse-compatible with V, I have included a TIO link for V.

Explanation

dW                      " Delete up to the next word (removes the first name)
$                       " Go to the end of the line
B                       " Go back one word
D                       " Delete it
share|improve this answer
    
Drat, you beat me to it. dWWD saves you one byte. – DJMcMayhem 2 days ago
5  
@DJMcMayhem That only works for exactly one middle name. – Martin Ender 2 days ago
    
What's the difference between dW and dw? – Duncan X Simpson yesterday
    
@DuncanXSimpson dW deletes until whitespace. dw deletes until non-word characters. – QPaysTaxes yesterday

05AB1E, 2 bytes

¦¨

Try it online!

If outputting a list of middle names isn't allowed, I'll change it.

share|improve this answer

Brain-Flak, 133 bytes

{{}((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}}{}{({}<>)<>}<>{{}((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}}{}{({}<>)<>}<>

Try it online!

132 bytes of code, plus 1 byte for the -c flag which allows ASCII input and output.

Unfortunately, this contains lots of duplicated code, but it would be really difficult to reuse. I'll look into it later. Here's an explanation:

#While True
{
    #Pop
    {}

    #Not equals 32
    ((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}

#Endwhile
}

#Pop the 0
{}

#Reverse Stack
{({}<>)<>}<>

#While True
{
    #Pop
    {}

    #Not equals 32
    ((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}

#Endwhile
}

#Pop the 0
{}

#Reverse Stack
{({}<>)<>}<>
share|improve this answer
    
86 bytes when you remove the comments. I added the first two and the last line. TIO – Riley 2 days ago
    
@riley Cool solution. Feel free to post it yourself! – DJMcMayhem 2 days ago

Haskell, 23, 17 9 bytes

init.tail

Takes and returns a list of strings. Try it online!

Drop first string, drop last string.

Edit: @Generic Display Name noted, that the input can be a list of strings, which saved 6 bytes.

Edit II: return list of strings instead of a single string

share|improve this answer
    
It looks like input as a list is allowed, so drop the words for -5 bytes – Generic Display Name 2 days ago
    
@GenericDisplayName: Oh, didn't notice. Thanks! – nimi 2 days ago
    
My Ohm answer and the Mathematica answer both output lists of strings as well, so you could probably drop unwords. for -8 bytes. – Nick Clifford 2 days ago
    
@NickClifford: yes, I saw that myself and asked the OP for clarification. – nimi 2 days ago

Python, 24 bytes

lambda n:n.split()[1:-1]

Try it online string input!

Input Format: string


Python 2, 16 bytes

lambda n:n[1:-1]

Try it online list input!

Input Format: List

share|improve this answer
    
You should edit the title with Python instead of Python 2, because it works for Python 3 as well, was just about to post that. – Mr. Xcoder 2 days ago
    
You are right! Thanks. – Keerthana Prabhakaran 2 days ago
    
@L3viathan since OP hasn't mentioned the output format must be a string, and since the input format is allowed to be a list, print a list cannot be considered as a wrong result! – Keerthana Prabhakaran 2 days ago
    
According to the comments on the question, you can both input and output a list of strings. Save a bunch of bytes lambda n:n[1:-1] – Luke Sawczak yesterday
1  
Since you can read from STDIN, can replace the lambda with input() (Python 3 only) – BallpointBen yesterday

Mathematica, 10 bytes

Rest@*Most

An unnamed function that accepts and returns a list of strings.

Rest discards the the last element, Most discards the first element, @* is function composition. Swapping Rest and Most or using right-composition /* instead would also work. This beats indexing via #[[2;;-2]]& by one byte.

share|improve this answer

Brain-Flak, 86 bytes

(()()){({}[()]<{{}((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}}{}{({}<>)<>}<>>)}{}

Try it online!

Most of this code comes from this answer. If you like my solution you should upvote that one as well.

#Push 2
(()())

#Loop twice
{({}[()]<

  #While not a space
  {
      #Pop
      {}

      #Not equals 32
      ((((()()()()){}){}){}[{}](<()>)){{}{}(<(())>)}{}

  #Endwhile
  }

  #Pop the 0
  {}

  #Reverse Stack
  {({}<>)<>}<>

#End loop twice
>)}{}
share|improve this answer
    
Well done! I didn't think about holding a counter for pushing later, so I spent too long thinking about how to loop across different stacks. – DJMcMayhem yesterday

AWK, 17 10 bytes

Saved 7 bytes thanks to @steve!

$NF=$1=x;1

Try it online!

Explanation:

$NF=    set last word to
$1=     set first word to
x       an empty variable, ie empty string
1       default action, ie print everything
share|improve this answer
    
Can be shaved down to 11 bytes, $NF=$1="";1 – steve 2 days ago
2  
Or 10 using $NF=$1=x;1 – steve 2 days ago
1  
@steve what does 1 do? I'm not so good at AWK :) – betseg 2 days ago
    
1 just means take the default action, that is, to print $0. – steve 2 days ago

JavaScript (ES6), 22 bytes

Takes and outputs an array of strings.

([_,...a])=>a.pop()&&a

Test cases

let f =

([_,...a])=>a.pop()&&a

console.log(f(["Samantha", "Vee", "Hills"]))
console.log(f(["Bob", "Dillinger"]))
console.log(f(["John", "Jacob", "Jingleheimer", "Schmidt"]))
console.log(f(["Jose", "Mario", "Carasco-Williams"]))

String version (27 bytes)

Takes and outputs a string. The output string is either a single space if no middle name was found, or the middle names with leading and trailing spaces.

s=>(/ .* /.exec(s)||' ')[0]

let f =

s=>(/ .* /.exec(s)||' ')[0]

console.log(f("Samantha Vee Hills"))
console.log(f("Bob Dillinger"))
console.log(f("John Jacob Jingleheimer Schmidt"))
console.log(f("Jose Mario Carasco-Williams"))

share|improve this answer

Java 7, 74 bytes

String f(String s){return s.substring(s.indexOf(' '),s.lastIndexOf(' '));}

Java 8, 49 bytes

s->s.substring(s.indexOf(' '),s.lastIndexOf(' '))

Function which identifies the first occurrence of the space character and the last one and extracts the middle. The resulting string is prefixed by a space character (at the time of the posting, OP hasn't clarified if leading spaces are allowed), which can be eliminated by adding .trim() to the code for an extra cost of 7 bytes.

Compared to C#, Java has the advantage of specifying the end index instead of sub-string length, which brings down the byte count.

share|improve this answer

Groovy, 19 bytes

{it.split()[1..-2]}

Explanation:

{        
 it                  all closures have an implicit argument called "it"
   .split()          splits by spaces by default. Returns an array of words
           [1..-2]   take the whole array from the second index (1) to the penultimate index (-2). Implicitly return
                  }

A closure / anonymous function

share|improve this answer
1  
Welcome to PPCG! Can you take a list of strings as input to skip the .split()? – Martin Ender 2 days ago
    
Martin Ender Yes, if you assume that the input will always be a list of strings then {it[1..-2]} would work. – staticmethod 2 days ago

PHP, 37 Bytes

<?=join(" ",array_slice($argv,2,-1));

-4 bytes for an output as array

print_r(array_slice($argv,2,-1));

PHP, 42 Bytes

echo trim(trim($argn,join(range("!",z))));

PHP, 50 Bytes

echo preg_filter("#(^[^\s]+ |[^\s]+$)#","",$argn);
share|improve this answer

Perl 5, 27 18 bytes

Need to run with -n option.

/ (.+) /&&print$1

Try it online!

Wanted to do something similar in sed first, but, unfortunately, it doesn't support non-greedy quantifier. It is needed in case middle name is more than one word.

Edit

-9 bytes thanks to Dada.

Non-greedy quantifier is not needed anymore, among with some other things.

share|improve this answer
    
/ (.+) /&&print$1 should be sorter. Great to see some new people golfing with Perl! – Dada 2 days ago
    
@Dada Thanks for the tip! It's actually my first time ever writing in Perl. Do you know why print if s| (.+) |\1| doesn't work? To me it looks similar to what you wrote. – Max Lawnboy 2 days ago
    
print if s| (.+) |\1| replaces the middle part with... the middle part! (minus the spaces before and after), so it doesn't work. On the other side, what I suggested only matches the middle part and print only it ($1). – Dada 2 days ago

Javascript (ES6) 49 16 bytes

Edit:

a=>a.slice(1,-1)

Try it online!

ungolfed:

function(name) {
  return a.slice(1, -1); //start at the second item and end at the second to last item
};

I forgot some of the simple properties of slice, and that the input can be an array. Thanks to @Neil and @fəˈnɛtɪk I was able to remove 27 bytes. Still not really competing.

Original:

This isn't really competing but here's a Javascript solution:

a=>{a=a.split(' ');return a.slice(1, a.length-1)}

This creates an anonymous function equal to:

function(name) {
  let name = name.split(' '); //first middle last -> [first, middle, last]
  return name.slice(1, name.length - 1); //get the second item to the second to last item in the array.
}

How I golfed it

This is a pretty simple golf. I turned the function into an arrow function. Then I "minified" the code. This included renaming name into a single character(a in this case) and removing the let decloration of the variable.

Snippet

var a=b=>b.slice(1,-1)

//example code

let form = document.querySelector('#form'),
    input = document.querySelector('#input'),
    result = document.querySelector('#result');

form.addEventListener('submit', (event) => {
  event.preventDefault();
  let value = input.value.split` `;
  result.textContent = a(value).join(' ');
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<form id="form">
  <div class="input-group">
    <input id="input" class="form-control" />
    <div class="input-group-btn">
      <button class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

<p id="result"></p>

Hope this helps anyone who is stuck on the challenge.

share|improve this answer
    
The length - is unnecessary, as slice already accepts negative lengths as being relative to the end. This means that you no longer need the intermediate variable, so you can turn your arrow function from a block into an expression. – Neil 2 days ago
    
Thanks. I always forget that little trick. – David Archibald 2 days ago
    
Actually the way it works is that -1 is the last but one that you need here. – Neil 2 days ago
    
It is also slice(1,-1). slice(1,-2) removes two from the end. – fəˈnɛtɪk 2 days ago
    
You can also assume that you were passed an array to begin with, which lets you just perform slice and you are done. – fəˈnɛtɪk 2 days ago

Retina, 11 bytes

^\S+ |\S+$

Try it online!

Matches the first word (including the space after it) and the last word, and removes both of them.

If I/O can be a linefeed-separated list, it can be done in 8 bytes instead:

A1`
G-2`

Try it online!

share|improve this answer

Röda, 9 bytes

{_[1:-1]}

Try it online!

Not a very interesting solution. Takes a list from the stream and returns the middle names.

21 bytes and I/O:

{[(_/" ")[1:-1]&" "]}

Try it online!

This uses / (split) and & (join).

share|improve this answer

Matlab, 81, 79, 78 Bytes

function x=a(s)
s=strsplit(s);x='';for i=2:numel(s)-1,x=horzcat(x,s{i},32);end

Takes in an input string, s, is split (by the default delimiter, whitespace char) into a cell array, from which the middle element is accessed. Then the middle elements are concatenated, or an empty string is returned.

Edit: thanks to Luis Mendo for saving 3 bytes!

share|improve this answer
    
I am not a smart man! edited. – Owen Morgan 2 days ago
    
You can't use nnz on a cell array, but I did the other two changes :) – Owen Morgan 2 days ago
    
Suggested Edit by Ankit, who doesn't have enough rep to comment. (55 bytes): function x=a(s) s=strsplit(s);x=strjoin(s(2:end-1));end – mbomb007 yesterday

C#, 67 bytes

s=>s.Substring(s.IndexOf(' ')+1,s.LastIndexOf(' ')-s.IndexOf(' '));

Anonymous function which identifies the first occurrence of the space character and the last one and extracts the middle. It also extracts a trailing space, which can be removed at the cost of 2 bytes.

Full program with test cases:

using System;

namespace WhatsMyMiddleName
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> f =
            s=>s.Substring(s.IndexOf(' ')+1,s.LastIndexOf(' ')-s.IndexOf(' '));

            Console.WriteLine(f("Jo Jean Smith"));          // "Jean"
            Console.WriteLine(f("Samantha Vee Hills"));     // "Vee"
            Console.WriteLine(f("Bob Dillinger"));          // ""
            Console.WriteLine(f("John Jacob Jingleheimer Schmidt"));// "Jacob Jingleheimer"
            Console.WriteLine(f("Jose Mario Carasco-Williams"));    // "Mario"
            Console.WriteLine(f("James Alfred Van Allen")); // "Alfred Van"
        }
    }
}
share|improve this answer
    
@Florian You are welcome to post your own answer (or to comment to give adrianmp some tips), but please leave the current answer in tact and let the author make their own changes. – Not that Charles yesterday

Kotlin, 39 bytes

s.filterIndexed{i,j->i!=0&&i!=s.size-1}

Try it online!

i.e.

s.filterIndexed{ index, value -> index != 0 && index != (s.size - 1) }
share|improve this answer

VBA, 69 bytes

Sub m(n)
f=InStr(1,n," ")
Debug.?Mid(n,f+1,InStrRev(n," ")-f)
End Sub
share|improve this answer

Ruby, 24 13 bytes

p ARGV[1..-2]

Saved 11 bytes thanks to Piccolo pointing out that array-like output is allowed.

Takes the name as separate command line arguments, e.g.:

$ ruby script.rb John Jacob Jingleheimer Schmidt

or

$ ruby -e 'p ARGV[1..-2]' John Jacob Jingleheimer Schmidt

Previous code (outputs a proper string):

puts ARGV[1..-2].join" "
share|improve this answer
    
@Jordan Good idea - however with $><<ARGV[1..-2].join" " it complains about the " " being unexpected, so I'd have to add parentheses - which would add 1 byte in the end. – Flambino 2 days ago
    
Ah, of course. My bad. – Jordan yesterday
    
The original poster said that the output can be an array, so you can shave some characters off by just changing your code to puts ARGV[1..-2], just so you know. – Piccolo yesterday
    
@Piccolo Huh? I don't see that anywhere? If true; p ARGV[1..-2] for 13 bytes - just looks nothing like the output in OPs challenge – Flambino yesterday
    

Golang, 152 81 bytes

import ."strings"
func f(x string)[]string{var k=Fields(x);return k[1:len(k)-1];}

It takes input as "Samantha Vee Hills" (with double quotes) and return the middle name to the stdout.

Try it Online!

Edit: @Dada, note that the "function as answer is allowed" shorten my code 71 bytes. a big thanks!

share|improve this answer
    
Welcome on the site. Submitting a function as answer is allowed, so you could shorten your code by doing something like this: Try it online!. Have a look at the Tips for Golfing in Go! – Dada yesterday
    
thank you @Dada for tips and shorten my code. It was my first code entry. – ersinakyuz yesterday

Python 2, 42 19 16 Bytes

lambda n:n[1:-1]

Try it online! Thanks to @Kritixi Lithos for saving 23 bytes! Thanks @math_junkie for saving 3 more bytes. For input, put each part of the name as a string within a list like so:

["Samantha", "Vee", "Hills"]

And yes, the OP has approved a list to be a valid input.

Explanation

lambda n:n[1:-1]    # Returns only the middle elements... pretty literal here
share|improve this answer
1  
print input()[1:-1] is shorter – Kritixi Lithos 2 days ago
    
Spring slicing sure is tricky – Anthony Pham 2 days ago
    
lambda n:n[1:-1] is even shorter – math_junkie 2 days ago
1  
I may have tried with a wrong input. But when I tried, with Samantha Vee Hills as input in repl.it link that you've shared, this just prints amantha Vee Hill which is definitely not the output required. – Keerthana Prabhakaran 2 days ago
2  
Names will always have at least 2 space-separated parts is the first point of the question right. – Keerthana Prabhakaran 2 days ago

C++, 91 bytes

#import<list>
#import<string>
void f(std::list<std::string>&n){n.pop_front();n.pop_back();}

Takes input as a reference to a list of strings and modifies the list directly.

Try it online!

share|improve this answer

Jelly, 2 bytes

ḊṖ

Try it online!

This works as a non-inline link (i.e. function), not a full program.

'John','Jacob','Jingleheimer','Schmidt''Jacob','Jingleheimer'

As a full program, it would be 3 bytes: ḊṖK, which prints a space-separated middle name.

share|improve this answer

Pyth, 2 bytes

Pt

Online interpreter

share|improve this answer

APL (Dyalog), 8 6 bytes

¯1↓1↓⊢

Try it online!

This is a tacit train.

Explanation

When is a dyad, it returns all but the first/last n elements of its vector argument.

   1↓                  ⍝ Remove the first element in the 
     ⊢                 ⍝ right argument
¯1↓                    ⍝ and remove the last element
share|improve this answer

Bash, 31 19 bytes

echo ${@:2:$[$#-2]}

Try it online!

Breakdown:

Prints parameter expansion from second to second last element.

Edit

Removed commented code from breakdown section, since it's now just one line long.

-12 bytes thanks to Neil

share|improve this answer
    
Can you not just echo the expression in the first line directly? – Neil 2 days ago
    
@Neil How can I do that, if the result of the expression is not $@? – Max Lawnboy 2 days ago
    
Surely set -- assigns the result of the expression to $@? – Neil 2 days ago
    
@Neil Took me way too long to understand what you mean. This is embarrasing... – Max Lawnboy 2 days ago

REXX, 42 bytes

arg a
say delword(delword(a,words(a)),1,1)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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