Print unique characters
Write a program to print all the unique characters in a given sentence.
The sentence should have only alphabets .
If the sentence is not valid display the message "Invalid Sentence".
If unique characters are not found print "No unique characters".
If unique characters are found print those characters as shown in sample output.
Answers
Program in Java:
import java.util.*;
public class MyClass
{
static int check(char A[], int l)
{
for(int i = 0; i < l; i++)
{
if((A[i] >= 0 && A[i] <= 31) || (A[i] >= 33 && A[i] <= 64) || (A[i] >= 91 && A[i] <= 96) || (A[i] >= 123 && A[i] <= 127))
{
return 1;
}
}
return 0;
}
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
String s;
System.out.print("Enter a sentence : ");
s = Sc.nextLine();
char A[] = s.toCharArray();
int l = A.length;
int flag = 0;
if(check(A, l) == 1)
{
System.out.println("Invalid Sentence");
}
else
{
for(int i = 0; i < l; i++)
{
int count = 0;
for(int j = 0; j < l; j++)
{
if(A[i] == A[j])
{
count++;
if(count > 1)
{
break;
}
}
}
if(count == 1)
{
flag = 1;
System.out.println(A[i]);
}
}
if(flag == 0)
{
System.out.println("No unique characters");
}
}
}
}
Output 1:
Enter a sentence : java is an object oriented programming language
v
s
b
c
d
p
l
u
Output 2:
Enter a sentence : Welcome to 12house
Invalid Sentence
Answer:
all test cases passed.
Explanation: