import java.io.*;
class Unique_Characters //Class Name
{
public static void main()throws IOException
{
int i,l,check=0;
char ch;
String str,str1="",str2="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
str=br.readLine();
str=str+" "; //Adding an extra space for extraction of words.
l=str.length(); //Finding the length for running the loop.
for(i=0;i<l;i++)
{
ch=str.charAt(i); //picking up the Character from the String
if(ch!=' ')
{
str1=str1+ch; //forming a word from the characters picked
}
else if(unique(str1)) //checking for unique
{
System.out.print(str1+" "); //if, then print the word
check=1; // Once found changing the indicator to 1
str1=""; // making it blank again for the next word to be extract
}
else
{
str1=""; //If not unique then making it blank for next word
}
} // For loop ends
if(check==0) //If check, remains 0 then none of the words are unique
{
System.out.println("None of the words are of unique characters");
}
} //main method ends
static boolean unique(String cpstr) //method to check String Unique or not
{
int i,j,l,count=0;
l=cpstr.length();
char c,ca;
for(i=0;i<l;i++)
{
c=cpstr.charAt(i);
count=0;
for(j=0;j<l;j++)
{
ca=cpstr.charAt(j);
if(c==ca)
{
count++;
}
}
if(count>1)
{
return false;
}
}
return true;
}
} // Class Ends
Explanation to the method
/*A method name "unique" is coded with the return type as Boolean (true or false).
So it will return "true" if its is a unique word, else it will return "false" if it is not unique.
The method is done based on Linear Search Algorithm, we search for a character in a word,
Once with first letter and then second, gradually with all the letters present in the word.
For each phase of search we keep a counter,if the counter is increased more than once
(note: Every letter will have a frequency of 1 for unique word), then the word is not unique,
We stop further check and return "false", from the method to the Main method.
After all the letters searched, if the counter still remains < 2 then we say its unique.
.................................................................................................
Example 1 : Lets take the word "school"
For the first letter 's' count = 1,
second letter 'c' also count = 1 so on ...... but when it comes to 'o' then the count = 2, which means
search is found with occurrence twice , so it it not a unique number.
.................................................................................................
Example 2 : Lets take the word "computer"
For the first letter 'c' count = 1,
second letter 'o' also count=1 so on ...... for all the occurrence the count is 1, which means
the word is unique.
.................................................................................................*/
Write a program in Blue J to input a sentence and print those words whose all characters are unique.
Programming