KnowledgeBoat Logo

Computer Science

A string is said to be valid if it contains pair of parenthesis having text / alphabet such as (TY) and the string is said to be invalid if it contains nested parenthesis such as (Y (UP)).

For example:
SUN (A(X) RISE) BEGINS FROM (RT) EAST is an "Invalid" string because in this string nested parenthesis are present, but SUN (A) RISE BEGINS FROM (RT) EAST is a "Valid" string because there is no nested parenthesis.

Write a program to:

  1. Read a string / sentence and convert in capitals.
  2. Check the validity of the given string.
  3. If the string is valid, output the given string omitting the portion enclosed in brackets otherwise, output a message "Sorry, and invalid string".

Test your program for the given sample data and also some other random data:

Sample Data:

Input:
SUN (a) RISE BEGINS FROM (RT) EAST
Output:
SUN RISE BEGINS FROM EAST

Input:
SUN (A (X) RISE) BEGINS FROM (RT) EAST
Output:
Sorry, an invalid string

Input:
COM(IPX)PUTER IS (MY) JUNK (GOOD) SYSTEM
Output:
COMPUTER IS JUNK SYSTEM

Java

Java String Handling

4 Likes

Answer

import java.util.*;

public class KboatParenthesisCheck
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string:");
        String str = in.nextLine();
        str = str.toUpperCase();
        int len = str.length();
        
        StringBuffer sb = new StringBuffer();
        
        int open = -1;  //Contains index of opening parenthesis
        
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (ch == '(') {
                if (open != -1) {
                    System.out.println("Sorry, an invalid string");
                    return;
                }
                
                open = i;
            }
            
            if (open == -1) {
                sb.append(ch);
            }
            
            if (ch == ')') {
                if (open == -1) {
                    System.out.println("Sorry, an invalid string");
                    return;
                }
                
                open = -1;
            }
            
            
        }
        
        /*
         * Using StringTokenizer to remove 
         * extra spaces between words
         */
        
        StringTokenizer st = new StringTokenizer(sb.toString());
        while (st.hasMoreTokens()) {
            System.out.print(st.nextToken());
            System.out.print(" ");
        }

    }
}

Output

BlueJ output of A string is said to be valid if it contains pair of parenthesis having text / alphabet such as (TY) and the string is said to be invalid if it contains nested parenthesis such as (Y (UP)). For example: SUN (A(X) RISE) BEGINS FROM (RT) EAST is an "Invalid" string because in this string nested parenthesis are present, but SUN (A) RISE BEGINS FROM (RT) EAST is a "Valid" string because there is no nested parenthesis. Write a program to: (a) Read a string / sentence and convert in capitals. (b) Check the validity of the given string. (c) If the string is valid, output the given string omitting the portion enclosed in brackets otherwise, output a message "Sorry, and invalid string". Test your program for the given sample data and also some other random data: Sample Data: Input: SUN (a) RISE BEGINS FROM (RT) EAST Output: SUN RISE BEGINS FROM EAST Input: SUN (A (X) RISE) BEGINS FROM (RT) EAST Output: Sorry, an invalid string Input: COM(IPX)PUTER IS (MY) JUNK (GOOD) SYSTEM Output: COMPUTER IS JUNK SYSTEMBlueJ output of A string is said to be valid if it contains pair of parenthesis having text / alphabet such as (TY) and the string is said to be invalid if it contains nested parenthesis such as (Y (UP)). For example: SUN (A(X) RISE) BEGINS FROM (RT) EAST is an "Invalid" string because in this string nested parenthesis are present, but SUN (A) RISE BEGINS FROM (RT) EAST is a "Valid" string because there is no nested parenthesis. Write a program to: (a) Read a string / sentence and convert in capitals. (b) Check the validity of the given string. (c) If the string is valid, output the given string omitting the portion enclosed in brackets otherwise, output a message "Sorry, and invalid string". Test your program for the given sample data and also some other random data: Sample Data: Input: SUN (a) RISE BEGINS FROM (RT) EAST Output: SUN RISE BEGINS FROM EAST Input: SUN (A (X) RISE) BEGINS FROM (RT) EAST Output: Sorry, an invalid string Input: COM(IPX)PUTER IS (MY) JUNK (GOOD) SYSTEM Output: COMPUTER IS JUNK SYSTEMBlueJ output of A string is said to be valid if it contains pair of parenthesis having text / alphabet such as (TY) and the string is said to be invalid if it contains nested parenthesis such as (Y (UP)). For example: SUN (A(X) RISE) BEGINS FROM (RT) EAST is an "Invalid" string because in this string nested parenthesis are present, but SUN (A) RISE BEGINS FROM (RT) EAST is a "Valid" string because there is no nested parenthesis. Write a program to: (a) Read a string / sentence and convert in capitals. (b) Check the validity of the given string. (c) If the string is valid, output the given string omitting the portion enclosed in brackets otherwise, output a message "Sorry, and invalid string". Test your program for the given sample data and also some other random data: Sample Data: Input: SUN (a) RISE BEGINS FROM (RT) EAST Output: SUN RISE BEGINS FROM EAST Input: SUN (A (X) RISE) BEGINS FROM (RT) EAST Output: Sorry, an invalid string Input: COM(IPX)PUTER IS (MY) JUNK (GOOD) SYSTEM Output: COMPUTER IS JUNK SYSTEM

Answered By

1 Like


Related Questions