Is there a way to create a program in java which takes 2 input strings and gives the resultant string as the character taken from both the strings alternatively.
Example : input S1 = apple input S2 = pears
Output = apppelres
I was able to write this program using the inbuilt functions.But can the same be written without using the inbuilt functions? Can any one please provide some information on this?
akshitha [surl='http://www.javaranch.com/unit-testing.jsp' class='api' title='article: evil unit testing wrote:test[/surl]]I was able to write this program using the inbuilt functions.But can the same be written without using the inbuilt functions? Can any one please provide some information on this?
This sounds to me like one of those ridiculous interview questions where you have to understand what the interviewer means by "inbuilt".
You're using Java. Everything you do is inbuilt. Creating a String is inbuilt; and you certainly can't read from a File (or the keyboard) without using an inbuilt function.
The only possibility I can think of is that Strings can be converted to char with the toCharArray() method (inbuilt), and maybe they want to see if you can write a decent method to take two character arrays and produce a third that contains only the unique letters.
Of course, you then have to create a new String with that...
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Heh. Gotta sympathize with Winston here. What do you mean by "inbuilt"? Also, what do you mean by "string"? In your decription of your problem, you haven't really said what the type of S1 and S2 is. If you want to treat them as arrays of bytes, you can simply index them alternately, with something like this:
Note, btw, that the output here is appepalres, rather than your example of apppelres (and your output is only nine letters long, while your two inputs have five letters each).
Now, this might look like it doesn't use functions, but even the subscripting of S1 and S2 implies something based on some code built into Java somewhere, so there's really no way to do anything without an "inbuilt" function.
Maybe if you said more about the type of data you are working with, and what restrictions you are under, someone could be more helpful.