Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string example issue

Posted on 2014-10-05
7
Medium Priority
?
278 Views
Last Modified: 2014-10-07
Hi,

I am trying below challenge

http://codingbat.com/prob/p199720

I did not understand challenge itself to proceed further. please advise


Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".

startOz("ozymandias") → "oz"
startOz("bzoo") → "z"
startOz("oxx") → "o"
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 1000 total points
ID: 40362523
It's rather a weird problem.  But it's asking you to check the first character of the string and only return it if it's an 'o'.  Then check the second character and only return that if it's a 'z'.

Here's a possible approach to solving it.  See if you can figure out how it works.  For instance, why do we check the str.length()?  Maybe you can find another way to write it?

public String startOz(String str) {
  String result = "" ;
  if (str.length() > 0 && str.charAt(0) == 'o') {
     result += "o" ;
  }
  if (str.length() > 1 && str.charAt(1) == 'z') {
     result += "z" ;
  }
  return result;
}

Open in new window


Doug
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 40363080
>> I did not understand challenge itself to proceed further. please advise
>> Given a string, return a string made of the first 2 chars (if present),
>> however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".

So basically they want you to return the string consisting of the first two characters of the given String.
But there's an extra condition for both characters. The first must be an 'o' and the second a 'z'.

Here's one of the many possible solutions:

public String startOz(String str) {
  String result = "";
  
  if (str.length()>0) { // does the given string contain at least one character?
     if (str.startsWith("o")) { // does the given string start with an "o"?
        result = result + "o";
     }
     str = str.substring(1); // chop off the first character of the given string
  }
  
  if (str.length()>0 && str.startsWith("z")) { // does the (chopped off) given string contain yet another character and is it a "z"?
    result = result + "z";
  }
  
  return result; 
}

Open in new window


PS.
The above code of dpearson is just what is given on http://codingbat.com/prob/p199720.
I wonder how that does help?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40366393
what happens if i pass

startOz("z")  

also
startOz(" z")

also
startOz("bz")
please advise
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 28

Expert Comment

by:dpearson
ID: 40367204
startOz("z") should return "" (z is not in the 2nd position)
startOz(" z") should return "z" (z is in the 2nd position)
startOz("bz") should return "z" (z is in the 2nd position)

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40367524
public class Test7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test=startOz("z"); //should return "" (z is not in the 2nd position)
	System.out.println("test is-->"+test);
	String test2=startOz(" z");
	System.out.println("test is-->"+test);
	String test3=startOz("dz");
	System.out.println("test is-->"+test);
	}

	
	public static String startOz(String str) {
		  String result = "";
		  
		  if (str.length()>0) { // does the given string contain at least one character?
		     if (str.startsWith("o")) { // does the given string start with an "o"?
		        result = result + "o";
		     }
		     str = str.substring(1); // chop off the first character of the given string
		  }
		  
		  if (str.length()>0 && str.startsWith("z")) { // does the (chopped off) given string contain yet another character and is it a "z"?
		    result = result + "z";
		  }
		  
		  return result; 
		}
}

Open in new window


I ran above code

I got output different as below

test is-->
test is-->
test is-->

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40367527
public class Test7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test=startOz("z"); //should return "" (z is not in the 2nd position)
	System.out.println("test is-->"+test);
	String test2=startOz(" z");
	System.out.println("test is-->"+test);
	String test3=startOz("dz");
	System.out.println("test is-->"+test);
	}

	
	public static String startOz(String str) {
		  String result = "" ;
		  if (str.length() > 0 && str.charAt(0) == 'o') {
		     result += "o" ;
		  }
		  if (str.length() > 1 && str.charAt(1) == 'z') {
		     result += "z" ;
		  }
		  return result;
		}
}

Open in new window


I even tried above code and got similar output
0
 
LVL 7

Author Comment

by:gudii9
ID: 40367529
public class Test8 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test=startOz("z"); //should return "" (z is not in the 2nd position)
	System.out.println("test is-->"+test);
	String test2=startOz(" z");
	System.out.println("test is-->"+test2);
	String test3=startOz("dz");
	System.out.println("test is-->"+test3);
	}

	
	public static String startOz(String str) {
		  String result = "";
		  
		  if (str.length()>0) { // does the given string contain at least one character?
		     if (str.startsWith("o")) { // does the given string start with an "o"?
		        result = result + "o";
		     }
		     str = str.substring(1); // chop off the first character of the given string
		  }
		  
		  if (str.length()>0 && str.startsWith("z")) { // does the (chopped off) given string contain yet another character and is it a "z"?
		    result = result + "z";
		  }
		  
		  return result; 
		}
}

Open in new window


i think i forgot to change test2, test3 in sysout
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
A solution for Fortify Path Manipulation.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question