Computer Science, asked by shivangik289, 4 hours ago

Question

Print unique charactersWrite 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.


Sample Input 1:

Enter the sentence:

java is an object oriented programming language


Sample Output 1:

Unique characters:

v

s

b

c

d

p

l

u


Sample Input 2:

Welcome to 12house


Sample Output 2:

Invalid Sentence​

Answers

Answered by wajahatkincsem
0

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

#SPJ2

Similar questions