Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » » » » Find Repeated Character From An Array of String In Java/Selenium/OATS


You have a array of strings e.g ["abcd","aabcd",xyz", "ccbc"] find the strings in
which characters are repeated.Output would be ["aabcd","ccbc"].

Logic:: Need to check if the current position and the next position are having the same character or not.If they have same character they are repetitive characters.


//You have a array of strings e.g ["abcd","aabcd",xyz", "ccbc"] find the strings in 
//which characters are repeated.Output would be ["aabcd","ccbc"].
//Author:Animesh



public class RepeatString {
 
 static String[] names={"abcd","aabcd","xyz", "cdbbc"};
 
 public static void main(String[] args) throws InterruptedException {
  for(int i=0;i<names.length;i++)
  {
   if(repeatCalulator(names[i]))
   {
    System.out.println(names[i]);
   }
  }
  
  
 }
 
 public static boolean repeatCalulator(String name)
 {
  boolean flag=false;
  int sizeoftheString=name.length();
  for(int j=1;j<sizeoftheString;j++)
  {
  if(name.charAt(j-1)==name.charAt(j))
  {
   
   flag=true;
   break;
   
  }
    
  }
  return flag;
  
 }

}




Image source:www.javaclass.info

«
Next
Newer Post
»
Previous
Older Post

2 comments:

  1. String str="HAPPYTESTING";
    String str2="";
    String temp1="";
    String temp2="";
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    for(int j=0;j<str.length();j++)
    { temp1=""+str.charAt(j);
    temp2=""+str.charAt(i);
    if(temp1.equalsIgnoreCase(temp2))
    count++;
    }
    if(count==1){
    count=0;
    System.out.println("Non Repeatative Character is:"+str.charAt(i));
    }
    else{
    if(!str2.contains(""+str.charAt(i)))
    {
    str2=str2+str.charAt(i);
    System.out.println("Repeatative Character is:"+str.charAt(i));System.out.print("");
    }
    count=0;
    }

    }
    }

    ReplyDelete
    Replies
    1. Thanks Hari for more code..Happy Testing

      Delete