Java String Exercises: Check two given strings whether any one of them appear at the end of the other string
Java String: Exercise-71 with Solution
Write a Java program to check two given strings whether any one of them appear at the end of the other string (ignore case sensitivity).
Sample Solution:
Java Code:
import java.util.*;
public class Main
{
public boolean anyStringAtEnd(String stng1, String stng2)
{
stng1 = stng1.toLowerCase();
int aLen = stng1.length();
stng2 = stng2.toLowerCase();
int bLen = stng2.length();
if (aLen < bLen)
{
String temp = stng2.substring(bLen - aLen, bLen);
if (temp.compareTo(stng1) == 0)
return true;
else
return false;
} else
{
String temp = stng1.substring(aLen - bLen, aLen);
if (temp.compareTo(stng2) == 0)
return true;
else
return false;
}
}
public static void main (String[] args)
{
Main m= new Main();
String str1 = "pqrxyz";
String str2= "xyz";
System.out.println("The given strings are: "+str1+" and "+str2);
System.out.println("Is one string appears at the end of other? "+m.anyStringAtEnd(str1,str2));
}
}
Sample Output:
The given strings are: xyz and pqrxyz Is one string appears at the end of other? true The given strings are: pqrxyz and xyz Is one string appears at the end of other? true
Pictorial Presentation:
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to find lexicographic rank of a given string.
Next: Write a Java program to return true if a given string contain the string 'pop', but the middle 'o' also may other character.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework