aspose file tools*
The moose likes Beginning Java and the fly likes Java String Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java String Program" Watch "Java String Program" New topic
Author

Java String Program

akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

When you say "inbuilt functions" do you mean the methods of the String class? If so, then I don't think that's possible. (I don't think it's a useful exercise for a student either.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8404
    
  23

akshitha test wrote: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...

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

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.
akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
Thanks all for the reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java String Program