Given a string s, return a substring containing all characters except the first and the last. The length of the s will be greater than 2.
Examples:
Input: s = "Hello"
Output: ell
Explanation: The first and last character are ignored.
Input: s = "World"
Output: orl
Explanation: The first and last characters are ignored.
Table of Content
Using Character Traversal - O(n) Time O(n) Space
The idea is to traverse the string from the second character to the second-last character and append each character to a new string. This new string will contain all characters except the first and last ones.
#include <iostream>
#include <string>
using namespace std;
// Function to return the sliced string
string sliceString(string &s)
{
// Stores the resultant substring
string res = "";
// Traverse from second character to second-last character
for (int i = 1; i < (int)s.length() - 1; i++)
{
res += s[i];
}
return res;
}
// Driver Code
int main()
{
string s = "World";
cout << sliceString(s);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to return the sliced string
char* sliceString(char* s)
{
// Stores the resultant substring
char res[100] = "";
int j = 0;
// Traverse from second character to second-last character
for (int i = 1; i < strlen(s) - 1; i++)
{
res[j++] = s[i];
}
res[j] = '\0';
return strdup(res);
}
// Driver Code
int main()
{
char s[] = "World";
char* result = sliceString(s);
printf("%s", result);
free(result);
return 0;
}
public class GfG {
// Function to return the sliced string
public static String sliceString(String s)
{
// Stores the resultant substring
String res = "";
// Traverse from second character to second-last
// character
for (int i = 1; i < s.length() - 1; i++) {
res += s.charAt(i);
}
return res;
}
// Driver Code
public static void main(String[] args)
{
String s = "World";
System.out.println(sliceString(s));
}
}
# Function to return the sliced string
def sliceString(s):
# Stores the resultant substring
res = ""
# Traverse from second character to second-last character
for i in range(1, len(s) - 1):
res += s[i]
return res
# Driver Code
if __name__ == "__main__":
s = "World"
print(sliceString(s))
using System;
public class GfG {
// Function to return the sliced string
public static string sliceString(string s)
{
// Stores the resultant substring
string res = "";
// Traverse from second character to second-last
// character
for (int i = 1; i < s.Length - 1; i++) {
res += s[i];
}
return res;
}
// Driver Code
public static void Main()
{
string s = "World";
Console.WriteLine(sliceString(s));
}
}
// Function to return the sliced string
function sliceString(s) {
// Stores the resultant substring
let res = "";
// Traverse from second character to second-last character
for (let i = 1; i < s.length - 1; i++) {
res += s[i];
}
return res;
}
// Driver Code
let s = "World";
console.log(sliceString(s));
Output
orl
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Built-in Substring/Slicing Function - O(n) Time and O(n) Space
The idea is to directly use the built-in substring/slicing function provided by the language. Since we need to ignore the first and last characters, we start from index
1and takelength - 2characters.
Built-in Substring/Slicing Functions in Different Languages:
C:
- C does not provide a built-in string slicing function. We can use functions like strncpy() to copy the required portion of the string into a new character array.
- The substring starts from index 1 and has length strlen(s) - 2.
C++:
- C++ provides the substr() function of the string class to extract a substring.
- We can directly use s.substr(1, s.length() - 2) to remove the first and last characters.
Python:
- Python supports string slicing using the slice operator [:].
- The expression s[1:-1] returns all characters except the first and the last.
JavaScript:
- JavaScript provides methods such as slice() and substring() for extracting substrings.
- The required substring can be obtained using s.slice(1, -1), which excludes the first and last characters.
Java:
- Java provides the substring() method of the String class for extracting a part of a string.
- The required substring can be obtained using s.substring(1, s.length() - 1).
C#:
- C# provides the Substring() method of the String class.
- We can start from index 1 and extract s.Length - 2 characters using s.Substring(1, s.Length - 2).
#include <iostream>
#include <string>
using namespace std;
// Function to return the sliced string
string sliceString(string &s)
{
// Extract substring excluding first and last character
return s.substr(1, s.length() - 2);
}
// Driver Code
int main()
{
string s = "World";
cout << sliceString(s);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to return the sliced string
char* sliceString(char* s)
{
static char result[100];
strncpy(result, s + 1, strlen(s) - 2);
result[strlen(s) - 2] = '\0';
return result;
}
// Driver Code
int main()
{
char s[] = "World";
printf("%s", sliceString(s));
return 0;
}
public class GfG {
// Function to return the sliced string
public static String sliceString(String s)
{
// Extract substring excluding first and last
// character
return s.substring(1, s.length() - 1);
}
// Driver Code
public static void main(String[] args)
{
String s = "World";
System.out.println(sliceString(s));
}
}
# Function to return the sliced string
def sliceString(s):
# Extract substring excluding first and last character
return s[1:-1]
# Driver Code
s = "World"
print(sliceString(s))
using System;
class GfG {
// Function to return the sliced string
public static string sliceString(string s)
{
// Extract substring excluding first and last
// character
return s.Substring(1, s.Length - 2);
}
// Driver Code
static void Main()
{
string s = "World";
Console.WriteLine(sliceString(s));
}
}
// Function to return the sliced string
function sliceString(s) {
// Extract substring excluding first and last character
return s.substring(1, s.length - 1);
}
// Driver Code
let s = "World";
console.log(sliceString(s));
Output
orl
Time Complexity: O(n)
Auxiliary Space: O(n)