DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #1 - String Peeler

Hey, everyone. We've decided to host a daily challenge series. We'll have a variety of challenges, ranging in difficulty and complexity. If you choose to accept the task, your goal is to write the most efficient function you can for your language. Bonus points for any features you add!

I’ll start you off with a simple, straightforward challenge. Our first one comes from user @SteadyX on CodeWars.

Your goal is to create a function that removes the first and last letters of a string. Strings with two characters or less are considered invalid. You can choose to have your function return null or simply ignore.

The fundamentals are important, it only gets more difficult from here.

Happy coding!


Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Oldest comments (113)

Collapse
 
vguleaev profile image
Vladislav Guleaev • Edited
function removeChar(str){
 return str.substr(1, str.length - 2)
};
Collapse
 
ben profile image
Ben Halpern • Edited

Ruby


def remove_first_and_last(string)
  raise "Invalid" if string.size < 3
  string[1..string.size-2]
end
Collapse
 
databasesponge profile image
MetaDave 🇪🇺

I think you can get away with string[1..-2] there, Ben.

Collapse
 
taillogs profile image
Ryland G • Edited

JavaScript

(someString) => someString.length > 2 ? someString.slice(1, -1) : undefined;
Enter fullscreen mode Exit fullscreen mode

Python

someString[1:-1] if len(someString) > 2 else None
Enter fullscreen mode Exit fullscreen mode

C++

someString.size() > 2 ? someString.substr(1, someString.size() - 1) : null;
Enter fullscreen mode Exit fullscreen mode

C

int trimString(char someString[])
{
  if (strlen(someString) > 2) {
    char *copy = malloc((strlen(someString) - 2) * sizeof(char));
    memmove (copy, someString + 1, strlen(someString) - 2);
    printf("%s", copy);
    free(copy);
    return 0;
  }
  return 1;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rafaacioly profile image
Rafael Acioly

Let people do something 😂

Collapse
 
jeromedeleon profile image
Jerome De Leon

HAHAHA damn. Let me finish my code LOL.

Collapse
 
highcenburg profile image
Vicente G. Reyes • Edited

Python

def remove(yo):
    if yo <= 2:
        print("Not enough characters in string")
    else:
    return yo[1:-1]
Collapse
 
martyhimmel profile image
Martin Himmel

PHP

function remove_string_ends($str) {
    if (strlen($str) <= 2) {
        return null;
    }
    return substr($str, 1, -1);
}
Collapse
 
claudioscatoli profile image
Claudio Scatoli • Edited

I like that substr trick with the -1, didn't think of that!

How about this one liner?

<?php

function trimThis(string $str)
{
    return (mb_strlen($str) <= 2) ? null : substr($str,1,-1);
}


Use mb_string to support multibyte chars, such as Chinese

Also typehinted the argument, you never know...


This one is even shorter, possible only if the arg is typehinted tho

<?php
function trimThis(string $str) {
    return substr($str,1,-1) ?: null;
}

When the string is shorter than 2, substr returns false;
when the length is 2, substr returns "".

In both cases it's false, the the return is null.


Edit: many typos, I'm on mobile :/

Collapse
 
coreyja profile image
Corey Alexander • Edited

Ruby

Basic

def remove_first_and_last(some_string)
  raise 'Not long enough' unless some_string.length > 2

  some_string[1..-2]
end

Extra

def remove_first_and_last_n_characters(some_string, chars_to_remove: 1)
  raise 'Not long enough' unless some_string.length > (chars_to_remove * 2)

  start_index = chars_to_remove
  end_index = -1 - chars_to_remove
  some_string[start_index..end_index]
end
Collapse
 
coreyja profile image
Corey Alexander • Edited

Rust

fn remove_first_and_last(string: &str) -> &str {
    remove_first_and_last_n_chars(&string, 1)
}

fn remove_first_and_last_n_chars(string: &str, chars_to_remove: usize) -> &str {
    if string.len() <= (2 * chars_to_remove) {
        panic!("Input string too short")
    }
    let start = chars_to_remove;
    let end = string.len() - 1 - chars_to_remove;

    &string[start..end]
} 

fn main() {
    println!("Ans: {}", remove_first_and_last("Hello, world!"));
    println!("Ans: {}", remove_first_and_last_n_chars("Hello, world!", 2));
    println!("Ans: {}", remove_first_and_last_n_chars("aa", 1));
    println!("Ans: {}", remove_first_and_last_n_chars("aa", 2));
}

View it in the Rust Playground here: play.rust-lang.org/?version=stable...

Collapse
 
jordonr profile image
Jordon Replogle

Perl?

$testing = "Happy coding!";
print &peeler($testing) . "\n";

sub peeler {
    my $str = shift;
    if(length($str) > 2) {
        return substr($str, 1, -1);
    }
}

Collapse
 
choroba profile image
E. Choroba

Or maybe just

sub peeler { shift =~ s/^.|.$//gr }
Collapse
 
werner profile image
Werner Echezuría

Rust

fn trim_string(string: &str) -> &str {
    if string.len() <= 2 {
        panic!("Input string too short");
    }
    &string[1..string.len() - 1]
}
Collapse
 
torianne02 profile image
Tori Crawford

Ruby

def remove_char(s)
  raise "invalid string" if s.size < 3

  return s[1..-1].chop
end

Some comments may only be visible to logged-in visitors. Sign in to view all comments.