Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

MATLAB Program for UNIQUE WORD

A string is said to be 'Unique' if none of the letters present in the string are repeated. Write a program to accept a string and check whether the string is Unique or not.

Input: COMPUTER

Output: Unique String

Input: APPLICATIONS

Output: Not a Unique String

We assume that the input String contains only ASCII characters (which includes all letters, numbers and special symbols that can be typed using the keyboard).

Prerequisite:



CODE to check UNIQUE String or not:


clc;

x=input('Enter the string:');
y=double(x);
l=length(y);
for i=1:length(y)
    if(y(i)>=97 && y(i)<=122)
        y(i)=y(i)-32;
    end
end
a=sort(y);
t=1;
for i=1:(length(a)-1)
    m=a(i);
    n=a(i+1);
    if(m~=n)
        t=t+1;
    end
end
if(t==l)
    disp('Yes');
else
    disp('No');
end

Explanation:






Some OUTPUTS (you can try with your own examples):





JAVA CODE:

import java.io.*;
class Gham
{
public static void main(String args[])throws IOException
{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string:");
String x=r.readLine();
int l=x.length();
char y[]=new char[l];
for(int i=0;i<l;i++)
{
y[i]=x.charAt(i);
}
for(int i=0;i<l;i++)
{
if(y[i]>='a' && y[i]<='z')
{
y[i]=(char)(y[i]-32);
}
}
char temp;
for(int i=0;i<l-1;i++)
{
for(int j=0;j<l-1-i;j++)
{
if(y[j]>y[j+1])
{
temp=y[j];
y[j]=y[j+1];
y[j+1]=temp;
}
}
}
char m;
char n;
int t=1;
for(int i=0;i<l-1;i++)
{
m=y[i];
n=y[i+1];
if(m!=n)
{
t=t+1;
}
}
if(t==l)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}

Some OUTPUTS (you can try with your own examples):





No comments

Popular Posts

Followers