Java: Read a string, if the first or last characters are same return the string without those characters otherwise return the string unchanged
Write a Java program to read a given string and return the string without the first or last characters if they are the same, otherwise return the string without the characters.
Visual Presentation:
Sample Solution:
Java Code:
Sample Output:
The given strings is: testcricket The new string is: estcricke
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to remove the first and last characters of a string if they are identical.
- Write a Java program to conditionally trim a string’s boundaries based on whether the first and last characters match.
- Write a Java program to check if the first and last characters of a string are the same, then return the string without them.
- Write a Java program to remove matching boundary characters from a string and output the resulting substring.
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to read a string and if a substring of length two appears at both its beginning and end,return a string without the substring at the beginning otherwise, return the original string unchanged.
Next: Write a Java program to read a string and return the string without the first two characters. Keep the first character if it is 'g' and keep the second character if it is 'h'.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics