7. Compare String
String s1 = "ABCD”;
String s2 = "ABCD”;
if(s1 == s2)
System.out.println("Equal");
else
System.out.println("NOT Equal");
In Java, when the“==” operator is used
to compare2 objects, it checks to see if
the objects refer to the same place in
memory.
8. String s1 = new String("ABCD”);
String s2 = new String("ABCD”);
if(s1 == s2)
System.out.println("Equal");
else
System.out.println("NOT Equal");
9. To check if strings have same data, use
equals
String s1 = new String("ABCD");
String s2 = new String("ABCD");
if(s1.equals(s2))
System.out.println("Equal");
else
System.out.println("NOT Equal");
10. String Concatination
System.out.println(3 + 5);
System.out.println(3 + 5 + "abcd");
System.out.println("abcd" + 3 + 5);
System.out.println("abcd" + 3 * 5);
What is the output of these
statements?
15. Count Number of Unique Characters
https://www.hackerrank.com/challenges/string-
construction
Editor's Notes
EQUAL
public class StringAssignment {
public static void main(String [] args) {
String src = "SIKANDER";
String dest;
dest = src;
System.out.println(dest);
System.out.println(src.hashCode() +" " + dest.hashCode());
dest = "BANGALORE";
System.out.println(src.hashCode() +" " + dest.hashCode());
}
}
public class StringConcatenation {
public static void main(String [] args) {
String firstName = "MOHAMMED";
String lastName = "SIKANDER";
String fullName = firstName + lastName;
System.out.println("First Name : " + firstName);
System.out.println("Last Name : " + lastName);
System.out.println("Full Name : " + fullName);
}
}
NOT EQUAL
NOT EQUAL
8
8abcd
Abcd35
abcd15
First case print in lowercase
Second one prints in Uppercase as str is not modified.
import java.util.Scanner;
public class StringConcatenation {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the input String ");
String s = in.next();
in.close();
int len = s.length();
int numberOfUpperCase = 1;
for(int i = 0 ; i < len ; i++)
{
//Access Individual Character
char c = s.charAt(i);
//Check if its uppercase character.
if(Character.isUpperCase(c) == true)
{
numberOfUpperCase++;
}
}
System.out.println("Number of Uppercase characters : " + numberOfUpperCase);
int numberOfWords = numberOfUpperCase + 1;
System.out.println("Number of words = " + numberOfWords);
}
}
import java.util.Scanner;
public class Pangram {
static int countUniqueCharacters(String sentence)
{
int lenOfSentence = sentence.length();
boolean [] alphabetFound = new boolean[26];
for(int i = 0 ; i < lenOfSentence ; i++){
if(sentence.charAt(i)!= ' '){
alphabetFound[ sentence.charAt(i) - 'a' ] = true;
}
}
int uniqueCharacters = 0;
for(int i = 0 ; i < 26 ; i++)
if(alphabetFound[i] == true)
uniqueCharacters++;
return uniqueCharacters;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int a0 = 0; a0 < n; a0++){
String sentence = in.next();
int uniqueCharacters = countUniqueCharacters(sentence);
System.out.println(uniqueCharacters);
}
}
}