Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Once I wrote a JavaScript program that would take as input a string and a character and would remove all characters except for the first one and the character given as input, one by one.

For example, computing this with inputs codegolf.stackexchange.com and e for the character yields:

codegolf.stackexchange.com
cdegolf.stackexchange.com
cegolf.stackexchange.com
ceolf.stackexchange.com
celf.stackexchange.com
cef.stackexchange.com
ce.stackexchange.com
cestackexchange.com
cetackexchange.com
ceackexchange.com
ceckexchange.com
cekexchange.com
ceexchange.com
ceechange.com
ceehange.com
ceeange.com
ceenge.com
ceege.com
ceee.com
ceeecom
ceeeom
ceeem
ceee

It keeps the first character and all es. All other characters are removed one by one.

Your task is to write a program (or function) that takes two inputs and outputs (or returns) a string that accomplishes this effect.

Specifications

  • You can assume that the string will not contain any newlines.
  • The second input will always be one character.
  • If the answer is in the form of a function, you may return an array of strings containing each line in the output.
  • The output can contain a trailing newline.

Test Cases

Test Cases, s:

Test Cases
Tst Cases
Ts Cases
TsCases
Tsases
Tsses
Tsss

Make a "Ceeeeeeee" program, e:

Make a "Ceeeeeeee" program
Mke a "Ceeeeeeee" program
Me a "Ceeeeeeee" program
Mea "Ceeeeeeee" program
Me "Ceeeeeeee" program
Me"Ceeeeeeee" program
MeCeeeeeeee" program
Meeeeeeeee" program
Meeeeeeeee program
Meeeeeeeeeprogram
Meeeeeeeeerogram
Meeeeeeeeeogram
Meeeeeeeeegram
Meeeeeeeeeram
Meeeeeeeeeam
Meeeeeeeeem
Meeeeeeeee

Hello World!, !:

Hello World!
Hllo World!
Hlo World!
Ho World!
H World!
HWorld!
Horld!
Hrld!
Hld!
Hd!
H!

Hello World!, z:

Hello World!
Hllo World!
Hlo World!
Ho World!
H World!
HWorld!
Horld!
Hrld!
Hld!
Hd!
H!
H

alphabet, a:

alphabet
aphabet
ahabet
aabet
aaet
aat
aa

upperCASE, e:

upperCASE
uperCASE
uerCASE
ueCASE
ueASE
ueSE
ueE
ue

This is , so the shortest code (in bytes) wins.

share|improve this question
13  
Kinda random, but +1 – TùxCräftîñg 2 days ago
13  
+1 for Meeeeeeeeegram – Flp.Tkc 2 days ago
    
In the case that it returns an array, do each of the elements have to include a trailing newline? – Brad Gilbert b2gills 2 days ago
    
@BradGilbertb2gills No. – Challenger5 2 days ago
    
Reminded me of SOMA Transmission #8 (look up on YouTube). "It's the weight of the s-e-e-e-ea!" – Malcolm yesterday

35 Answers 35

Vim, 27, 26 bytes

DJqq:t$|s/.\zs[^<C-r>"]<CR>@qq@qD

Try it online!

Input comes in this format:

e
codegolf.stackexchange.comk

I'm very proud of this answer because I don't typically use ex commands in my vim answers. My naive first approach is three bytes longer:

i:s/.\zs[^<Right>]<Esc>"addqqYp@a@qq@qdd

Less successful approaches:

i:s/.\zs[^<Right>]<Esc>"addqqYp@a@qq@qdd
i:s/.\zs[^<Right>]<CR>@q<Esc>"adkqqYp@aq@qdd
DJ:s/.\zs[^<C-r>"]<CR>uqqYp@:@qq@qdd
DJqq:t.|$s/.\zs[^<C-r>"]<CR>@qq@qdd

Explanation:

D                                   " Delete everything on this first line
 J                                  " Remove this empty line
  qq                                " Start recording into register 'q'
    :                               " Open the vim command line (for 'ex' commands)
     t$                             " Make another copy of this line
       |                            " And
         s/                         " Run a substitute command on the last line in the buffer. Remove:
           .                        "   Any character
            \zs                     "   Move the selection forward (so we don't delete the first one)
               [^<C-r>"]            "   Followed by anthing *except* for the character we deleted
                        <CR>        " Run the command
                            @q      " Call register 'q'
                              q     " Stop recording
                               @q   " Run the recursive macro
                                 D  " Delete our extra line
share|improve this answer
    
I think you got a typo there where the input is given there is a k too much :) – geisterfurz007 yesterday

Haskell, 50 bytes

w@(a:x)%c|(d,_:y)<-span(==c)x=w:(a:d++y)%c|0<1=[w]
share|improve this answer
2  
Can you explain the code? – bli 6 hours ago

Retina, 28 27 bytes

Byte count assumes ISO 8859-1 encoding.

;{G*1`
R1r`(?!^|.*¶?\1$)(.)

Try it online!

Explanation

;{G*1`

There's a lot of configuration here. The stage itself is really just G1`, which keeps only the first line, discarding the input character. * turns it into a dry run, which means that the result (i.e. the first line of the string) is printed without actually changing the string. { tells Retina to run both stages in a loop until the string stops changing and ; prevents output at the end of the program.

R1r`(?!^|.*¶?\1$)(.)

This discards the first character which a) isn't at the beginning of the input, b) isn't equal to the separate input character.

share|improve this answer

MATL, 20 16 bytes

y-f1X-"t[]@X@q-(

Try it online! Or verify test cases: 1, 2, 3, 4, 5.

Bonus: modified code to see the string being gradually shrunk (offline compiler):

y-f1X-"tt.3Y.XxD[]@X@q-(].3Y.XxDvx

enter image description here

Explanation

y        % Implicitly input string and char. Duplicate string onto top
-        % Subtract. Gives nonzero for chars in the input string that are
         % different from the input char
f        % Array of indices of nonzero values
1X-      % Remove 1 from that array. This gives an array of the indices of 
         % chars to be removed from the string
"        % For each
  t      %   Duplicate previous string
  []     %   Push empty array
  @      %   Push index of char to be removed. But this index needs to be 
         %   corrected to account for the fact that previous chars have
         %   already been removed...
  X@q-   %   ... So we correct by subtracting the 0-based iteration index
  (      %   Assign empty array to that position, to remove that char
         % Implicitly end for each
         % Implicitly display
share|improve this answer
1  
GIFS! gifs are cool! – Ander Biguri yesterday
1  
@AnderBiguri Suever's compiler is even cooler :-) It's still experimental, though – Luis Mendo yesterday

Perl 5, 29 bytes

I got 35 bytes using Strawberry Perl: 31 bytes, plus 1 for -nE instead of -e, plus 3 for space + -i (used for the single-letter input; the longer string is from STDIN).

chomp;say;s/(.)[^$^I]/$1/&&redo

However, I've no doubt this is doable without chomp; using <<<, which is 29 bytes, even though I can't test it myself using Strawberry.

say;s/(.)[^$^I]/$1/&&redo

Thus:

perl -im -nE'say;s/(.)[^$^I]/$1/&&redo' <<< "example"
share|improve this answer
    
You can just specify "no newline in the input" (which is how the second program works). If you badly need to remove newlines in the input, look into the -l option, which turns on an automatic newline handling mode in which print prints an additional newline (irrelevant here) and -p/-n input removes the newline (very relevant). Also, it's deprecated, but I think you can replace the ^I with a literal control-I for an extra byte of savings. Finally, I think s/.\K[^$^I]/redo/e would be one character shorter, although I'm not 100% sure that's a legal place to put a redo. – ais523 2 days ago
    
@ais523, thanks for the newline advice, but I guess I handled the issue well enough already. Re literal ^I, that's true for most of the control-letter variables, but not this one, IIRC. Re \K and putting redo in the replacement with /e, thanks! I'll test it when I have a chance to…. – msh210 2 days ago
    
... and it doesn't work. @ais523 – msh210 19 hours ago

Perl 6,  47 40  38 bytes

->\a,\b{a,{S/^(."{b}"*:)./$0/}...^{$^a eq $^b}}
->\a,\b{a,{S/^(."{b}"*:)./$0/}...^&[eq]}
{$^b;$^a,{S/^(."$b"*:)./$0/}...^&[eq]}

Expanded:

{       # lambda with two placeholder parameters 「$a」 and 「$b」

  $^b;    # declare second parameter

  $^a,    # declare first parameter, and use it to seed the sequence

  {       # bare block lambda with implicit parameter 「$_」
    S/      # string replace and return
      ^       # beginning of string
      (       # capture into 「$0」
        .       # the first character
        "$b"*   # as many 「$b」 as possible
        :       # don't allow backtracking
      )
      .       # any character ( the one to be removed )

    /$0/      # put the captured values back into place
  }

  ...^      # repeat that until: ( and throw away the last value )

  &[eq]     # the infix string equivalence operator/subroutine

}

The reason ...^ was used instead of ... is that &[eq] wouldn't return True until the last value was repeated.

share|improve this answer

Pip, 22 26 24 22 bytes

Lv+#Paa@oQb?++oPaRA:ox

Takes string as first command-line argument, character as second. Try it online!

Explanation

Loops over characters of input; if the character equals the special character, move on to the next one; if not, delete it and print the string.

An ungolfed version (a, b get cmdline args; o starts with a value of 1, x is ""):

P a         Print a
L #a-1      Loop len(a)-1 times:
 I a@o Q b   If a[o] string-eQuals b:
  ++o         Increment o
 E {         Else:
  a RA: o x   In-place in a, Replace char At index o with x (i.e. delete it)
  P a         Print a
 }

Golfing tricks:

  • The loop header for L is only evaluated once, so we can sneak the initial print in there. #Pa-1 won't work because P is low-precedence (it would parse as #P(a-1)), but we can rearrange it to v+#Pa, using the v variable preinitialized to -1.
  • The RA: operator returns the new value of a, so we can print that expression instead of having a separate Pa statement.
  • Now both of the branches of the if statement are single expressions, so we can use the ternary operator ? instead.
share|improve this answer

05AB1E, 26 25 bytes

¬ˆ[¦Ðg_#¬²k0Qi²ˆë¯J?,]¯J?

¬ˆ                         Put the first character into an array
  [                        While true
   ¦                       Remove the first character
    Ð                      Triplicate
     g_#                   if the string is empty, break
        ¬²k0Qi             if the first character is equal to the one specified in the input
              ²ˆ           Add it to the array
                ë          Else
                 ¯J?       Display the array
                    ,      Display the remaining string
                     ]     End while
                      ¯J?  Display the last string

Try it online!

Please note that ¬²k0Q could be rewritten ¬²Q, but for some reason it doesn't work when the current character is a quote mark: Q returns the actual string instead of a boolean and it causes an infinite loop.

This code can be golfed further since ¯J? is duplicated. Moving this part in the loop would remove the duplication and would also allow to drop the closing square bracket.

share|improve this answer

Python 2, 71 66 bytes:

f,m=input();k=f[0]
while f:a=f[0]==m;k+=f[0]*a;f=f[1+a:];print k+f

A full program. Takes 2 inputs through STDIN in the format '<String>','<Char>'.

Also, here is a recursive solution currently at 140 bytes:

Q=lambda c,p,k='',j=1,l=[]:c and Q(c[1:],p,k+c[0]*(j<2)+c[0]*(c[0]==p),j+1,l+[k+c])or'\n'.join(sorted({*l},key=l.index))+('\n'+k)*(k not in l)

This one should be called in the format print(Q('<String>','<Char>')).

share|improve this answer
    
I'm no python buff, but shouldn't this print only one line? – Conor O'Brien 2 days ago
    
@ConorO'Brien Yeah, I misread the post before. It's fixed now. – R. Kap 2 days ago

JavaScript (ES6), 74 bytes

(s,c)=>[...t=s.slice(1)].map(d=>c!=d?s+=`
`+s[0]+(t=t.replace(d,``)):s)&&s
share|improve this answer
    
I think this produces incorrect output for f('test cases', 's') (ending with stss, rather than tsss). I think this is because replace removes the first occurance so it removes the first t rather than the second t in the fourth iteration of the map loop. – Lmis yesterday
    
@Lmis Thanks for pointing that out, I think I was able to fix one of my versions for "only" a 7 byte penalty. – Neil 23 hours ago

c90, 129 125 bytes

with whitespace:

main(q, x)
{
    for (char **v = x, *a = v[1], *b = a, *c;*b++;)
        for (c = a; c == a | *c == *v[2] && *b != *v[2] && putchar(*c),
            ++c != b || *b != *v[2] && !puts(b););
}

without whitespace:

main(q,x){for(char**v=x,*a=v[1],*b=a,*c;*b++;)for(c=a;c==a|*c==*v[2]&&*b!=*v[2]&&putchar(*c),++c!=b||*b!=*v[2]&&!puts(b););}

ungolfed:

#include <stdio.h>
int main(int argc, char **argv)
{
    char *a = argv[1];
    for (char *b = a + 1; *b; b++) {
        if (*b == *argv[2]) {
            continue;
        }
        putchar(*a);
        for (char *c = a + 1; c != b; c++) {
            if (*c == *argv[2]) {
                putchar(*c);
            }
        }
        puts(b);
    }
}

This takes a pointer to the start of the string, and loops, iterating this pointer until it reaches the end of the string. Within the loop, it prints the first character, then any instances of the second argument it finds between the start of the string and the pointer. After this, it calls puts on the pointer, printing out the rest of the string.

This must be compiled on a system where sizeof(int) == sizeof(char*). +3 bytes otherwise.

This is the first time I've tried code golfing here, so I'm sure there are some optimizations to be made.

share|improve this answer

Java 7, 155 140 bytes

void c(String s,char c){System.out.println(s);for(int i=1;i<s.length();)if(s.charAt(i++)!=c){c(s.substring(0,i-1)+s.substring(i),c);break;}}

Ungolfed:

void c(String s, char c){
  System.out.println(s);
  for(int i = 1; i < s.length();){
    if(s.charAt(i++) != c){
      c(s.substring(0, i-1) + s.substring(i), c);
      break;
    }
  }
}

Test code:

Try it here.

class M{
  static void c(String s,char c){System.out.println(s);for(int i=1;i<s.length();)if(s.charAt(i++)!=c){c(s.substring(0,i-1)+s.substring(i),c);break;}}

  public static void main(String[] a){
    c("Make a \"Ceeeeeeee\" program", 'e');
  }
}

Output:

Make a "Ceeeeeeee" program
Mke a "Ceeeeeeee" program
Me a "Ceeeeeeee" program
Mea "Ceeeeeeee" program
Me "Ceeeeeeee" program
Me"Ceeeeeeee" program
MeCeeeeeeee" program
Meeeeeeeee" program
Meeeeeeeee program
Meeeeeeeeeprogram
Meeeeeeeeerogram
Meeeeeeeeeogram
Meeeeeeeeegram
Meeeeeeeeeram
Meeeeeeeeeam
Meeeeeeeeem
Meeeeeeeee
share|improve this answer
    
Wouldn't it be much shorter to not bother with a char[], and just use s.charAt()? – dpa97 yesterday
    
@dpa97 Ah, you're completely right. I used a foreach loop at first, but changed it to a regular for-loop. Forgot to remove the char-array. Thanks. – Kevin Cruijssen yesterday

Dyalog APL, 27 bytes

{×i←⊃1+⍸⍺≠1↓⎕←⍵:⍺∇⍵/⍨i≠⍳≢⍵}

is the excluded character, is the initial string

print argument; find index i of the first non- after the first char; if found, call recursively with i removed

share|improve this answer

C#, 122 117 112 bytes

IEnumerable F(string s,char c){for(int i=0;i<s.Length;++i)if(i<1||s[i]!=c)yield return i>0?s=s.Remove(i--,1):s;}

Ungolfed :

public IEnumerable F(string s, char c) {
    for (int i = 0; i < s.Length; ++i) {
        if (i < 1 || s[i] != c)
            yield return i > 0 ? s = s.Remove(i--, 1) : s;
    }
}

Returns a collection of strings.

share|improve this answer
1  
Nice trick with using not generic collection. But it won't work, if last char is not special char c. In that case loop will try to work forever. – paldir yesterday
1  
@paldir Woops, you're right ! Turning my brain on this time, found a better (and shorter !) way. – psycho yesterday
    
You could remove the parenthesis of the for loop to save 2 bytes. – PmanAce yesterday
    
@PmanAce Sorry, what do you mean ? Which parenthesis ? – psycho yesterday
    
public IEnumerable F(string s, char c) { for (int i = 0; i < s.Length; ++i) if (i < 1 || s[i] != c) yield return i > 0 ? s = s.Remove(i--, 1) : s; } – PmanAce 23 hours ago

Pyke, 26 19 bytes

jlFjR<Q/Q*jhR+jih>+

Try it here!

share|improve this answer

05AB1E, 26 24 23 bytes

Thanks @Kade for 2 bytes!
Thanks @Emigna for 1 byte!

¬UDvy²k0Êiy¡¬s¦yý«Xs«=¦

Uses the CP-1252 encoding. Try it online!

y²k0Ê could be y²Ê but the " messes it up.

This probably could be golfed more because « is repeated twice. Please leave a comment if you have any suggestions or ways to golf it down more.

share|improve this answer

Ruby, 148 139 97 90 83 77 bytes

a,c=$*;s=a.dup;i=1;p s;while s[i]do if s[i]!=c;s[i]="";p s;else i+=1;end end

Not sure if amateur code is accepted on this exchange but I'm interested in learning to code golf although I'm terrible at it, any help on how I'd get this program looking as small as the others here?

EDIT:

Replaced puts with p

Removed a tonne of whitespace and counted bytes correctly thanks to Wheat Wizard

Thanks to challenger5 went from s=gets.chop;c=gets.chop; to s,c=gets.chop,gets.chop;

replaced then with ; and gets.chop with gets[0] thanks Mhutter!

Taking input as command line variables now, eg. prog.rb helloworld l

I think this is as small as I can manage!

share|improve this answer
    
Welcome to the site! I am not an expert in Ruby golfing but it looks like you have extra whitespace. Particularly around the =s. For more comprehensive tips you can visit our tips page. – Wheat Wizard yesterday
    
The easiest way to remove bytes is to eliminate excess whitespace, for example s=gets.chomp. I'm not sure if you can do this in Ruby but in some languages like Python you can combine multiple assignments into one statement, like a,b,c=0,1,2. – Challenger5 yesterday
    
Hey thanks for the tip about whitespace read the ruby documentation and realised semicolons can replace them for ending statements :') as for making the s=gets.chop and c=gets.chop i cant do s,c=gets.chop or anything like that unfortunately theyre definitely the largest part of the code and I'd like to remove that lengthy statement.. – Ben Hili yesterday
    
You still have some extra spaces particularly before keywords (do,then and end), and around the fourth =. – Wheat Wizard yesterday
    
It looks like you are short changing yourself on the byte count. I only count 90 bytes myself. – Wheat Wizard yesterday

C#, 135 138 :( 137 bytes

Golfed:

IEnumerable<string>F(string s,char c){int i=1,l;for(;;){yield return s;l=s.Length;while(i<l&&s[i]==c)i++;if(i==l)break;s=s.Remove(i,1);}}

Ungolfed:

    IEnumerable<string> F(string s, char c)
    {
        int i = 1, l;

        for (;;)
        {
            yield return s;

            l = s.Length;

            while (i < l && s[i] == c)
                i++;

            if (i == l)
                break;

            s = s.Remove(i, 1);
        }
    }

Function returns collection of strings.

EDIT1: @psycho noticed that algorithm was not implemented properly.

EDIT2: Created variable for s.Length. One byte saved thanks to @TheLethalCoder.

share|improve this answer
1  
Won't work if the input char is present more than once in a row. Ex : codeegolf e would give ce instead of cee. – psycho yesterday
    
@psycho I swapped if with while and it works. – paldir yesterday
    
Better ! But it can be shorter. I'll post my own ! – psycho yesterday
1  
Create a variable for s.Length to save one byte: int i=1,l;for(;;){yield return s;l=s.Length;while(i<l&&s[i]==c)i++;if(i>=l)break;s=s.Remove‌​(i,1);}} – TheLethalCoder 9 hours ago

Mathematica, 78 bytes

Damn you Martin Ender, I was almost first :p

(i=2;a={c=#};While[i<=Length@c,If[c[[i]]==#2,i++,c=c~Drop~{i};a=a~Append~c]];a)&

Unnamed function; straightforward implementation with a While loop and a few temporary variables.

share|improve this answer
    
Oh, c'mon, we both know that Mathematica isn't imperative – LegionMammal978 2 days ago
    
<waves a white flag> – Greg Martin yesterday

JavaScript ES6, 89 bytes

I thought this would be an easy challenge, but I'm pretty sure I'm missing something here..

Uses recursion and returns an array of strings

(c,i=1,r)=>f=a=>a[i]?a[i++]==c?f(a):f(g=a.slice(0,i-1)+a.slice(i--),(r=r||[a]).push(g)):r

F=
  (c,i=1,r)=>f=a=>a[i]?a[i++]==c?f(a):f(g=a.slice(0,i-1)+a.slice(i--),(r=r||[a]).push(g)):r

G=_=>A.value.length>1 && (O.innerHTML = F(E.value)(A.value).join`
`)

G()
<input id=A oninput='G()' value='alphabet'>
<input id=E oninput='G()' value='a'>
<pre id=O>

share|improve this answer

JavaScript (ES6), 69

Returning an array of strings

s=>c=>[...s].map((x,i,z)=>i?x!=c&&(z[i]='',z.join``):s).filter(x=>x)

Returning a single string with newlines

s=>c=>[...s].map((x,i,z)=>i?x!=c?(z[i]='',`
`+z.join``):'':s).join``

F=
s=>c=>[...s].map((x,i,z)=>i?x!=c?(z[i]='',`
`+z.join``):'':s).join``

function update() {
  var s=S.value,c=C.value[0]
  O.textContent=F(s)(c)
}

update()
<input id=S value='Hello world!' oninput='update()'>
<input id=C value='!' oninput='update()'>
<pre id=O></pre>
                  

share|improve this answer
    
Slightly odd in that it doesn't take two arguments but one argument and then returns another function where you need to give the second argument. I'm not sure this was the expected behaviour. – MT0 yesterday
    
@MT0 I's odd but it's accepted for a function with 2 arguments, as it saves 1 byte. I'll give you a reference when I find one. Here it is meta.codegolf.stackexchange.com/a/8427/21348 – edc65 yesterday

Mathematica, 64 bytes

Most@FixedPointList[StringReplace[#,b_~~Except@a:>b,1]&,a=#2;#]&

Anonymous function. Takes two strings as input, and returns a list of strings as output. Works by repeatedly removing the first non-instance of the character.

share|improve this answer

Python 3, 67 63 Bytes

a,b=input(),input()
print(''.join([a[0]]+[x for x in a if x==b]))

It takes a as the string and b as the char, then goes through each letter in the string and adds it to the list if it's the first one or the character. Then it joins and prints.

EDIT: Fixed the problem mentioned by @R. Kap where it included copies of the first character as well, and shortened it to 63 bytes in the meantime.

share|improve this answer
    
As per this post, I think you can make the first line a,b=input(), but you would have to use Python 2, which would make it so that you could remove the parenthesis with print. You would provide the input like this 'Hello, World', 'o'. – nedla2004 2 days ago
    
Hmmm...for the first test case (codegolf.stackexchange.com,e) I just get the output cececec. – R. Kap yesterday
    
This solution doesn't output the intermediate strings – Lulhum 2 hours ago

PHP, 88 84 86 85 82 81 78 bytes

1 byte saved thanks to @IsmaelMiguel, 3 bytes thanks to @user59178, 3 bytes inspired by @user59178

while($b=substr("$argv[1]\n",$i++))$a>""&$b[0]!=$argv[2]?print$a.$b:$a.=$b[0];

takes input from command line arguments; run with php -r <code> '<string>' <character>


  • appends a newline to the input for an implicit final print.
    That adds 5 4 bytes of code, but saves on the output and an additional echo$a;.
share|improve this answer
1  
$argv[1]."\n" can be written as "$argv[1]\n" – Ismael Miguel 2 days ago
1  
As $b gets a newline added to it it will always by truthy as long as it has length >= 1. As such the ""< is unnecessary. – user59178 yesterday
    
You can save another byte by using a ternary in the substr() rather than assigning $b. – user59178 yesterday
    
@user59178 I didn´t really catch you there: I need the substr result for both the condition and the print; so I should assign it somewhere. But You inspired me. – Titus yesterday
    
I meant for(;$b=substr($b?:".$argv[1]\n",1);) but what you have now even better. – user59178 yesterday

C,86 83 bytes

char x[];i;m(char*v,int c){for(x[i]=*v;*v;)*++v-c?printf("%s%s\n",x,v):(x[++i]=c);}
share|improve this answer

TSQL, 127 bytes(Excluding variable definitions)

DECLARE @1 VARCHAR(100)='codegolf.stackexchange.com'
DECLARE @2 CHAR(1) = 'o'

DECLARE @ char=LEFT(@1,1)WHILE patindex('%[^'+@2+']%',@1)>0BEGIN SET @1=STUFF(@1,patindex('%[^'+@2+']%',@1),1,'')PRINT @+@1 END

Formatted:

DECLARE @1 VARCHAR(100) = 'codegolf.stackexchange.com'
DECLARE @2 CHAR(1) = 'o'
DECLARE @ CHAR = LEFT(@1, 1)

WHILE patindex('%[^' + @2 + ']%', @1) > 0
BEGIN
    SET @1 = STUFF(@1, patindex('%[^' + @2 + ']%', @1), 1, '')

    PRINT @ + @1
END
share|improve this answer

JavaScript [101 bytes]

f = phrase, t = key

a=[];a.push(f[0]);for(var i=0;i<f.length;i++)if(f[i]==t)a.push(f[i]);for(var p in a)console.log(a[p])
share|improve this answer

Python 3, 87 83 bytes

s=open(0).read()
p,c,s=s[0],s[-2],s[:-3]
for h in s:p+=s[:h==c];s=s[1:];print(p+s)

Explanation: I chose python 3 instead of python 2, because it allows to use file descriptors in open (and 0 is the descriptor of the standard input). In each loop iteration p keeps track of stored prefix. s[:h==c] works as follows: boolean expression h==c is casted into integer (0 or 1), and then the corresponding amount of characters from the beginning of s are taken.

P.S. The loop in the initial version was:

for h in s:
 if h==c:p+=h
 s=s[1:];print(p+s)
share|improve this answer
    
Defining a function will be much shorter. 62 characters: def _(s,c): p=s[0] for h in s:p+=s[:h==c];s=s[1:];print(p+s) (2 line breaks as needed) – Annan 2 days ago
    
This prints extra lines when c is encountered (e.g. cegolf.stackexchange.com is printed twice with c='e') – Jonathan Allan 2 days ago

JavaScript (ES2015) - 86 bytes

let f=([a,...s],c,r=[])=>r[0]==a+s?r:f(a+s.replace(eval(`/[^${c}]/`),''),c,[a+s,...r])
share|improve this answer
1  
Can you please provide a working snippet? I can't seem to get this to work – Bassdrop Cumberwubwubwub 2 days ago
    
It actually wasn't working to begin with. I forgot I intended to wrap the function with another. I've fixed it now. I'm on my iPad, procrastinating - don't even know how I'd add a working snippet lol – Sethi 2 days ago

Javascript ES6 - 61 Bytes

f=(a,b)=>a[0]+a.substring(1).split(RegExp(`[^${b}]`)).join``;

Accessing strings as arrays saves the day! (And a ton of bytes).

share|improve this answer
    
looks to me as if this would only return the last line – Titus yesterday
    
I missed that part of the spec - I'll have to go back to the drawing board. Thanks for pointing that out. – Marcus Dirr yesterday

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.