Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java String Program

 
akshitha Akki
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stevens Miller
Bartender
Posts: 1193
21
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic