31
\$\begingroup\$

Given an input string only containing the characters A-Z, a-z, and spaces, remove all occurrences of the uppercase and lowercase versions of the first character of the string (if the first character is A remove all As and as, if the first character is (space) remove all spaces), and print the output.

Example cases:

  • Testing Testing One Two Three -> esing esing One wo hree
  • Programming Puzzles and Code Golf -> rogramming uzzles and Code Golf
  • How much wood would a woodchuck chuck if a woodchuck could chuck wood -> ow muc wood would a woodcuck cuck if a woodcuck could cuck wood
  • {space}hello world -> helloworld
  • welcome to WATER WORLD -> elcome to ATER ORLD

This is , shortest code in bytes wins!

Notes:

  • Input will always be 2 or more valid characters.
  • Output will never be an empty string.
\$\endgroup\$
3
  • \$\begingroup\$ Do we need to handle the case where output is the empty string? What if input is the empty string? \$\endgroup\$
    – lirtosiast
    Commented Oct 24, 2015 at 17:23
  • \$\begingroup\$ @ThomasKwa you may assume output will alway be 2 or more characters \$\endgroup\$
    – GamrCorps
    Commented Oct 24, 2015 at 17:51
  • \$\begingroup\$ @ThomasKwa and output will never be empty \$\endgroup\$
    – GamrCorps
    Commented Oct 24, 2015 at 17:52

60 Answers 60

1
2
1
\$\begingroup\$

Retina, 16 bytes

i`(.)(?<=^\1.*)

Save the code with a trailing linefeed and run it with the -s flag.

How it works: the trailing linefeed makes this a replacement stage, such that any matches of the given regex are replaced with an empty string. The i turns on case-insensitive mode which also makes backreferences case-insensitive. Finally, the regex simply matches and captures a single characters and then checks whether the first character in the string is the same (up to case) using a backreference.

\$\endgroup\$
1
\$\begingroup\$

Gema, 19 characters

\A?=@set{c;?}
\C$c=

Sample run:

bash-4.3$ gema '\A?=@set{c;?};\C$c=' <<< 'Testing Testing One Two Three'
esing esing One wo hree

bash-4.3$ gema '\A?=@set{c;?};\C$c=' <<< ' Testing Testing One Two Three'
TestingTestingOneTwoThree
\$\endgroup\$
1
\$\begingroup\$

Bash, 35 characters

c=${1::1}
echo "${1//[${c^}${c,}]}"

Sample run:

bash-4.3$ bash rm-1st-char.sh 'Testing Testing One Two Three'
esing esing One wo hree

bash-4.3$ bash rm-1st-char.sh ' Testing Testing One Two Three'
TestingTestingOneTwoThree
\$\endgroup\$
1
\$\begingroup\$

TeaScript, 16 bytes

TeaScript is JavaScript with shortened property names.

x.g(x[0],'','i')

Try it online


TeaScript 3 came out and now this is 10 bytes.

xg(x░,u,'i

which compiles to:

xg(x[0],u,'i

which compiles to:

x.g(x[0],u,'i')

where u is preset to ""

\$\endgroup\$
2
  • \$\begingroup\$ TeaScript does not work for me in firefox. Test aht should be es ah, right? Second (friendly) TeaScript does work. \$\endgroup\$
    – Zereges
    Commented Oct 24, 2015 at 6:51
  • \$\begingroup\$ @Zereges weird. I'll use the other version while I see why that's not working \$\endgroup\$
    – Downgoat
    Commented Oct 24, 2015 at 14:52
1
\$\begingroup\$

Brainfuck, 72 bytes

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

Try it online.

Checks whether the differences between the first character and subsequent characters are divisible by 32.

\$\endgroup\$
1
\$\begingroup\$

Vim, 11 keystrokes

x:s/<C-r>-//gi

With a trailing newline

Although there already is a Vim answer, this one uses a different approach.

This deletes the first first character, which automatically gets stored in register -. Then we do a search and replace, replacing the deleted character (<C-r>-) with nothing (ie deleting it). The gi flags are used (global and case-insensitive).

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes

ćDš«м

Try it online!

Newer version than what Adnan used.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 7 6 bytes

żŒsḢ⁸ḟ

Try it online!

1 byte saved thanks to Erik The Outgolfer

How it works

żŒsḢ⁸ḟ - Main link. Argument: s (string) e.g. "ABCabc"

 Œs    - Swap the case                        "abcABC"
ż      - zip with the input                   ["Aa", "Bb", "Cc"]
   Ḣ   - take the first element               "Aa"
     ḟ - Filter those characters from...
    ⁸  -   the input                          "BCbc"

Original

ḟḢ;Œs$$ - Main link. Argument: s (string) e.g. "ABCabc"

 Ḣ      - Generate the first character of s    "A"
   Œs   - Generate the swapped case of ^       "a"
  ;     - Concatenate                          "Aa"
ḟ    $$ - Filter ^ from the input              "BCbc"
\$\endgroup\$
2
  • \$\begingroup\$ Alternatively for 6 bytes: żŒsḢ⁸ḟ \$\endgroup\$ Commented Oct 17, 2017 at 16:47
  • \$\begingroup\$ @EriktheOutgolfer oh, nice. Thanks! \$\endgroup\$ Commented Oct 17, 2017 at 16:52
1
\$\begingroup\$

Vyxal, 5 bytes

h:N"F

Try it Online!

Look ma, no Unicode!

h     # First character of (implicit) input
   "  # Paired with
 :    # A copy of itself
  N   # With case swapped
    F # Remove those characters from (implicit) input
\$\endgroup\$
1
\$\begingroup\$

Arturo, 31 bytes

$[s][l:lower s print l--@->l\0]

Try it

$[s][            ; a function taking an argument s
    l: lower s   ; assign lowercased s to l
    print        ; print
    l --         ; from l, remove...
    @->l\0       ; the first element of l (as a list)
]                ; end function
\$\endgroup\$
1
\$\begingroup\$

Japt, 3 bytes

kUÎ

Try it (includes all test cases)

kUÎ     :Implicit input of string U
k       :Case insensitively remove
 UÎ     :First character of U
\$\endgroup\$
2
  • \$\begingroup\$ A 1-byte built-in for “case insensitively remove”? That's wild… \$\endgroup\$
    – xigoi
    Commented Jan 28, 2023 at 2:29
  • 1
    \$\begingroup\$ That's a slightly simplified explanation of what it's doing in this solution, @xigoi. To provide a bit more detail: the k method takes a string as an argument and removes all occurrences of the characters in that string from the string it's applied to. By default it does so case insensitively but it can also take a second, optional argument to make it case sensitive. \$\endgroup\$
    – Shaggy
    Commented Jan 28, 2023 at 22:35
1
\$\begingroup\$

Raku, 17 bytes

{S:i:g/$(m/./)//}

Try it online!

  • S:i:g/...// returns a copy of the input string in which the regex between the first two slashes has been Substituted, case-insensitively and globally, with the content between the last two slashes, which is nothing--that is, the matches are deleted.
  • m/./ matches the first character of the input string, returning a Match object. Note that unlike many regular expression dialects, a dot matches any character, even a newline.
  • $(m/./) interpolates that first character into the regex as a plain string pattern.
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 6.5 bytes

+%~@%`)/_$`)$

Attempt This Online!

Interestingly, this only works because none of the ASCII codes of the allowed characters are divisible by each other. That's because I'm using % (modulo) to test if two characters are different.

\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 6 bytes

hẊoxPo

Attempt This Online!

Thunno 2 J, 8 bytes

ḢçRL'|jḄ

Attempt This Online!

More fun answer using regex.

Explanation

hẊoxPo  # Implicit input
h       # First character of the input
 Ẋ      # Store in x without popping
  o     # Remove this from input
   x    # Push x again
    P   # Swap its case
     o  # Remove this from the string
        # Implicit output
ḢçRL'|jḄ  '# Implicit input
Ḣ          # First character of the input
 ç         # Apply and collect results:
  R        #  Uppercase the character
   L       #  Lowercase the character
    '|j   '# Join the pair by "|"
       Ḅ   # Split the string on this regex
           # Implicit output, joined
\$\endgroup\$
0
\$\begingroup\$

Hassium, 57 Bytes

func main(){s=input().toLower()print(s.replace(s[0],""))}

Run online and see expanded here

\$\endgroup\$
1
  • 4
    \$\begingroup\$ This will change all uppercase letters into lowercase. E.g, the input HEY should be EY but not ey. \$\endgroup\$
    – Zereges
    Commented Oct 24, 2015 at 6:48
0
\$\begingroup\$

Julia, 40 bytes

t->t[find([(l=lowercase(t))...].!=l[1])]

l=lowercase(t) makes l store the string t reduced to lowercase. [l...] then converts the string into a character array. .!=l[1] compares that array element-wise with the first value of it, returning true if they don't match. find then converts the resulting boolean array into an array containing the true indices, and t[] then returns the desired string.

It feels like Julia should be able to do better than this, but so far I can't see it. It's a lot easier if you don't have to catch both upper- and lower-case instances of the letter.

\$\endgroup\$
0
\$\begingroup\$

Ceylon, 93 84

String r(String i)=>String{if(exists f=i[0])for(c in i.rest)if(c.offset(f)%32!=0)c};

This defines a function r which takes and returns a String, solving the task.

Formatted:

String r(String i) =>
        String {
    if (exists f = i[0])
        for (c in i.rest)
            if (c.offset(f) % 32 != 0)
                c };

The function creates and returns a string from an Iterable comprehension, composed of several if- and for-clauses.

The first if makes sure that the string is non-empty (if its empty, the iterable will be empty, thus the returned string too), and at the same time declares the f variable to be used later. (i[0] could have also been written i.first, but this way is shorter.) While the question says we can be sure the input has always length > 1, the compiler doesn't know this. (A different way of saying this would be to use an assert statement, but this would be even longer.)

The next for clause iterates over the remainder of the string (i.e. the string without its first character). Each item is named c.

The second if does the case-check. It turns out that in ASCII, the upper- and lower case letters have distance 32 from each other, so c.offset(f) % 32 != 0 is a slightly shorter way of writing c.uppercased != f.uppercased. (This would also filter other character, e.g. in the input strings Testing Testing One Two Three4, Programming Puzzles and 0Code0 Golf00, ((((How much wood would (a( woodchuck chuck (if a woodchuck could chuck (wood, @ hello` `world, 7welcome to WATER WORLD7 would also result in the example outputs in the question. But as the input is restricted to [A-Z a-z]+, we don't care.)

The last clause it simply the expression c – which will be included if the if clause's condition is true, and omitted otherwise.

The resulting iterable is passed to the String's class constructor, which iterates over it and creates the string.

As a bonus, here is a version which works for all of unicode:

String r(String i) =>
        String {
    if (exists f = i[0])
        for (c in i.rest)
            if (c.uppercased != f.uppercased)
                c };

After space-removal this is 93 bytes:

String r(String i)=>String{if(exists f=i[0])for(c in i.rest)if(c.uppercased!=f.uppercased)c};

It converts (beside the example phrases) also Übel wütet der Gürtelwürger to bel wtet der Grtelwrger.

\$\endgroup\$
0
\$\begingroup\$

Simplex v.0.6, 10 bytes

bC&0/gi//g
b          ~~ takes input as a string
 C  /  //  ~~ match as SiRegex
  &0       ~~ replace the first cell
     gi    ~~ globally, ignoring case,
           ~~ with the empty string
         g ~~ output the result

No regrets about adding a new type of Regular Expression. (SiRegex FTW!)

\$\endgroup\$
0
\$\begingroup\$

C(136)

#define t(x)(x+32*(x>>5==2)) 
char*h(char*a,int b,int*c){char*v=a[b+1]?h(a,b+1,c):0;return(b<=*c)?v:&(a[b]=a[b-(*c+=t(*a)==t(a[b]))]);}

Execution

void main(){

    char a[]="abac";int v=0;
    printf("%s",h((char*)a,0,&v));
    return;
}

Output

bc

  • the function is recursive which is called from the initial parameters: h(string,0,0) and returns the beginning of the resulting string.
\$\endgroup\$
0
\$\begingroup\$

Powershell, 62 60 44 bytes

$args-replace"$($args.substring(0,1))",""

Powershell of all things.

Edit: removed whitespace to save 2 bytes
Edit2: removed type accelerators to save 16 more bytes

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Hi and welcome to Programming Puzzles and Code Golf Stack Exchange (PPCG.SE for short)! Just FYI, the standard format of an answer starts with #[Language], [N] bytes as the first line where N is the length (in UTF-8 bytes) of your program. Otherwise, I, along with the rest of the PPCG.SE community wishes you good luck in your golfing adventure! \$\endgroup\$
    – GamrCorps
    Commented Oct 28, 2015 at 3:38
  • \$\begingroup\$ Thanks for the welcome and the advice. You barely beat the downvotes. Gotta love SE. I've been struggling to use regex instead of .net string handling, thinking it would save me some bytes, however, I can't seem to find a way to capture the first character AND search for it using $string.replace("...","") \$\endgroup\$
    – Xalorous
    Commented Oct 28, 2015 at 4:02
  • 1
    \$\begingroup\$ Can be further shortened to $argz-replace($argz[0]) (23 bytes). \$\endgroup\$ Commented Oct 18, 2017 at 0:43
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 6 chars / 13 bytes (non-competitive)

ïĥ/⏖î⍀

Try it here (Firefox only).

As does happen a lot, I beat everyone in char count but not in byte count :P

Explanation

ï is input, ĥ is replace, /...⍀ transpiles to /.../gi in JS, and ⏖î directly replaces the result of ï[0] in the compiled code.

\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 47 bytes

(lambda(s)(remove(aref s 0)s :test'char-equal))
\$\endgroup\$
0
\$\begingroup\$

J, 17 bytes

#~1-(={.)@tolower

Usage

   f =: #~1-(={.)@tolower
   f 'Testing Testing One Two Three'
esing esing One wo hree
   f 'Programming Puzzles and Code Golf'
rogramming uzzles and Code Golf
   f ' hello world'
helloworld
\$\endgroup\$
0
\$\begingroup\$

Rebol, 25 bytes

print replace/all s s/1{}

Alternative function at 28 bytes:

func[s][replace/all s s/1{}]
\$\endgroup\$
0
\$\begingroup\$

C#, 101 bytes

void m(string s){Console.Write(s.Replace(char.ToUpper(s[0]),'\0').Replace(char.ToLower(s[0]),'\0'));}

Sadly C# falls short on this one due to not having a case insensitivity option in Replace and not having an empty char literal

I tried using regex but it's more bytes (someone might manage shorter with it though)

\$\endgroup\$
0
\$\begingroup\$

Java, 96 bytes.

Java is an unlikely winner, it's pretty verbose even in a golfed form. Here |32 is to force lower-case comparison regardless of the actual value, doesn't affect space.

void m(String s){char[]c=s.toCharArray();for(char a:c)System.out.print((a|32)!=(c[0]|32)?a:"");}

At a slightly higher level of String class it would be more explicit (121 bytes):

void m(String s){String f=s.substring(0,1).toLowerCase();System.out.println(s.replaceAll("["+f+f.toUpperCase()+"]",""));}
\$\endgroup\$
0
\$\begingroup\$

Convex, 5 bytes, noncompeting

(_±+-

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 7 Bytes

¬DŠ-sš-

Explanation:

¬        # Take the first character of the input
 D       # Duplicate top of the stack
  Š      # Pop a,b,c and push c,a,b
   -     # Remove all occurencies of the first letter
    s    # Swap the two top elements
     š   # Swapcase the top of the stack
      -  # Remove all occurencies of the swapcased letter
         # Implicit: print top of the stack

Uses ISO 8859-1 encoding

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ¬Dš‚K is 5, AFAICT. \$\endgroup\$ Commented Oct 17, 2017 at 17:14
0
\$\begingroup\$

Uiua SBCS, 6 bytes

▽≠⊢.⌵.

Try it!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 55 bytes

lambda s:''.join(c*(c.lower()!=s[0].lower())for c in s)

Try it online!

Outgolfed, but I like the elegance :)

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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