Java > String-2 > sameStarChar (CodingBat Solution)

Problem:

Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.

sameStarChar("xy*yzz") → true
sameStarChar("xy*zzz") → false
sameStarChar("*xa*az") → true


Solution:

public boolean sameStarChar(String str) {
  int len = str.length();
  boolean found = true;
  
  for (int i = 0; i < len; i++) {
    String tmpString = str.substring(i,i+1);
  
    if (tmpString.equals("*") && i > 0 && i < len-1) {
      if (str.charAt(i-1) == str.charAt(i+1))
        found = true;
      else
        found = false;
    }
  }
  return found;
}


14 comments :

  1. public boolean sameStarChar(String str) {
    for (int i=1; i<str.length()-1; i++) {
    if (str.charAt(i) == '*') {
    if (str.charAt(i-1) != str.charAt(i+1)) return false;
    }
    }
    return true;

    ReplyDelete
  2. public boolean sameStarChar(String str) {
    for(int i=0; i=1 && str.charAt(i)=='*' && str.charAt(i-1)!=str.charAt(i+1)) return false;
    return true;
    }

    ReplyDelete
  3. public boolean sameStarChar(String str) {
    int star = str.lastIndexOf("*", str.length()-2);
    if(str.length()<3 || !str.contains("*")) return true;
    return str.charAt(star-1)==str.charAt(star+1);
    }

    ReplyDelete
  4. public static boolean sameStarChar(String str) {
    String input=str;
    for(int i=0;i<input.length();i++) {
    if(input.charAt(i)=='*'&&i!=0&&i!=input.length()-1) {
    if(input.charAt(i-1)==input.charAt(i+1)){
    return true;
    }
    }
    }
    return false;


    }

    ReplyDelete
  5. boolean res=true;int option=0;

    if(!str.contains("*"))
    {
    return true;
    }

    if(str.length()==0)
    {
    return true;
    }
    if(str.length()==1)
    {
    if(str.charAt(0)=='*')
    {
    return true;
    }
    }
    if(str.length()==2)
    {
    if(str.charAt(0)=='*'&&str.charAt(1)=='*')
    {
    return true;
    }
    }
    for(int i=1;i<str.length()-1;i++)
    {
    if(str.charAt(i)=='*')
    {

    if(str.charAt(i-1)==str.charAt(i+1))
    {
    // System.out.println(true);
    option=1;
    }
    if(str.charAt(i-1)!=str.charAt(i+1))
    {
    // System.out.println(true);
    option=2;
    }

    }


    }
    if(option==1)
    {
    return true;
    }
    return false;

    ReplyDelete
  6. public boolean sameStarChar(String str)
    {
    boolean result = true;
    int length = str.length();
    int pos = str.indexOf("*");

    do {
    if(pos>0 && pos0);

    return result;
    }

    ReplyDelete
  7. for(int i=0;i0&&i<str.length()-1))
    {
    if(str.charAt(i-1)!=str.charAt(i+1))
    return false;
    }
    }
    return true;

    ReplyDelete
  8. public String oneTwo(String str) {
    if( str.length() <= 2 ){
    return "";
    }
    return "" + str.charAt(1) + str.charAt(2) + str.charAt(0)
    + oneTwo(str.substring(3));
    }

    ReplyDelete
  9. public String oneTwo(String str) {
    return ( str.length() <= 2 ) ? ""
    :"" + str.charAt(1) + str.charAt(2) + str.charAt(0)
    + oneTwo(str.substring(3));
    }

    ReplyDelete
  10. public boolean sameStarChar(String str) {
    boolean res = true;
    for (int i=1;i<str.length()-1;i++) {
    if (str.charAt(i)=='*') {
    if (str.charAt(i-1) == str.charAt(i+1))
    res = true;
    else
    res = false;
    }
    }
    return res;
    }

    ReplyDelete
  11. public boolean sameStarChar(String str) {
    for (int i = 1; i<str.length()-1; i++) {
    if (str.charAt(indexOf("*")+1)!=str.charAt(indexOf("*")-1))) {
    return false;
    }

    }
    return true;
    }
    Why does this give a missing '}' or illegal start of expression error?

    ReplyDelete
    Replies
    1. one more brace at the end of the line 3. ) <--- such line

      Delete
  12. public boolean sameStarChar(String str) {
    if (!str.contains("*")) return true;


    else if (str.length()<3) {
    for (int i=0; i<str.length() ; i++){
    if (str.charAt(i)!='*') return false;
    }
    }


    for (int i=1; i<str.length()-1 ; i++){
    if (str.charAt(i)=='*' && str.charAt(i-1) != str.charAt(i+1)) return false;
    }
    return true;
    }

    ReplyDelete
  13. the shortest way:


    public boolean sameStarChar(String str) {
    for(int i = 1; i < str.length() - 1; i++){
    if(str.charAt(i) == '*' && str.charAt(i - 1) != str.charAt(i + 1)){
    return false;
    }
    }
    return true;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact