import java.util.*;
/*
*
* A string consists of parenthesis and letters. Write a program to validate all the parenthesis.
* Ignore the letters.eg.
* ((alf)ls) – valid
* )(dkk)() – invalid
*/
public class StringI_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter string :- ");
String str = sc.nextLine();
System.out.println("Entered string is :- " + str);
int countOpen = 0 ;
int countClose =0 ;
int sizeOfString = str.length();
if(str == null || sizeOfString == 0){
System.out.println("String is Empty");
System.exit(0);
}
if (str.charAt(0) != '(' && str.charAt(sizeOfString - 1) != ')' ){
System.out.println("First : Invalid String");
System.exit(0);
}
/*else
System.out.println("valid");*/
for(int i = 0 ; i< sizeOfString ; i++){
if(str.charAt(i) == '(' && i!=(sizeOfString-1) ){
if( str.charAt(i+1) == ')' ){
System.out.println("Second :Invalid String ");
System.exit(0);
}else{
countOpen++;
System.out.println("countOpen :- " + countOpen +" "+ str.charAt(i));
}
}else {
if(str.charAt(i) == ')' ){ //&& str.charAt(i+1) !=')'
countClose++;
System.out.println("countClose :- " + countClose +" "+ str.charAt(i));
}
}
}
if(countOpen == countClose){
System.out.println("Valid string");
}else{
System.out.println("Third : Invalid String");
}
}
}
A blog about new technology, Android, iOS, Mobile Automation, Appium, Web Automation, Selenium, JAVA, Shell Scipting, Powershell, Testing Tools, QA, Spring Boot, Free Software, How to design best frameworks etc..
Java : A string consists of parenthesis and letters. Write a program to validate all the parenthesis.
Subscribe to:
Posts (Atom)
πAnnouncement : Visit the new website/blog π
πππ Announcement : Visit the new website/blog πππ Dear readers, Thank you for reading my blog. If you could have used the little info...
-
How to find the UDID of Android Device What is UDID : It stands for Unique Device Identifier. UDID is required duri...
-
How to change language of device using commandLine Changing language in Android device on-the fly is needed when we are running or exe...
-
To setup Appium for mobile automation, you will need below items to be configured in your test environment. 1. Appium server : 1.1 D...
-
How to get the package name of Android application There are multiple ways in which we can obtain the package name of Android .apk file. ...