aspose file tools*
The moose likes Java in General and the fly likes write a java program without using any builtin functions(not even toString(),toCharArray(),toArray() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "write a java program without using any builtin functions(not even toString(),toCharArray(),toArray()" Watch "write a java program without using any builtin functions(not even toString(),toCharArray(),toArray()" New topic
Author

write a java program without using any builtin functions(not even toString(),toCharArray(),toArray()

vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
write a java program without using any built-in functions(not even toString(),toCharArray(),toArray()) FOR REVERSING THE STRING.......

TRIED TO USE DECLARATION IN STRING.... LIKEWISE, String str = "welcome to java world";

i want this to be reversed without using any built in functions... please provide the solution for this... Thanku in advance...
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Do you mean without using any of the methods of the String class or without using any methods of any class ?
vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
yes without using any of the java built in functions.... not even string functions also....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40

vinaykumar vinay wrote:yes without using any of the java built in functions.... not even string functions also....


If you can't call any of the string methods, then you can't extract the characters from the string. And if you can't do that, your program don't have the characters to reverse.... which of course, means that it is impossible.


BTW, what is the point of this exercise?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
Hey thank you for your view Henry,


its been asked in lot of interviews, i tried doing by using char array declarations.... like Char[] ch = char[20] {'a','b','c'};
can we do by using this type of declarations..??.. if so post the code , Thank you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40

vinaykumar vinay wrote:Hey thank you for your view Henry,


its been asked in lot of interviews, i tried doing by using char array declarations.... like Char[] ch = char[20] {'a','b','c'};
can we do by using this type of declarations..??.. if so post the code , Thank you


Well, if the "definition" of a string is a character array, then it is easy. You just need to reverse the elements of the array..... Why don't you give it a try? And if you run into any issues, post what you did, and we can give you a hint in the right direction.

Henry
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

Sure, you could declare an char[] array and pass that to the String constructor.
Then reverse that char[] array, which is possible without using any "built in functions", and use the result to create a second String instance, which would be the reverse of the first.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Henry Wong wrote:If you can't call any of the string methods, then you can't extract the characters from the string

Depends whether it means calling them directly in your code.
I was thinking you could create a StringBuilder instance using the String and then reverse the content of the StringBuilder. Obviously methods of the String class will be being called in the StringBuilder code, but your code is not calling them directly.

Obviously this is a moot point if the OP is not allowed to call any method in any class
vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
import java.util.*;
import java.io.*;
import java.lang.*;
class RevString
{
public static void main(String[] args)
{
System.out.println("welcome!");
int index = 0;
int Length = 0;
int j = 0;
char RevString ='\0';
char tempString = '\0';
char[] temp = new char[]{'a','b','c','d','e','f','\0'};
//String temp= "Welcome to cluster";
while('\0' != temp[index])
index++;
Length = index;
System.out.println("String Length = " + Length);

for(index = 0; index < Length;Length--,index++)
{

RevString = temp[index];

temp[index] = temp[Length-1];
temp[Length] = RevString;
tempString = temp[index];
System.out.println("reversed string isss " + temp[index]);
//System.out.println("Reversed string : "+ RevString);
}
}
}


check it out, i think there is some minute i done, please complete it...
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Surely to reverse a String you actually have to have a String in the first place. That code doesn't contain a String. All you are doing is reversing a char array.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Jelle Klap wrote:Sure, you could declare an char[] array and pass that to the String constructor.
Then reverse that char[] array, which is possible without using any "built in functions", and use the result to create a second String instance, which would be the reverse of the first.

If that's the answer then it seems like a bit of a cheat to me. If I was asked to write code that reversed a String, I would expect that the String was going to be supplied, not that I would have to create that as well.

It's like asking someone "Can you redecorate my house - and if you don't mind can you build the house first as well."
vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
Hello Adrian,

As i am new to programming world, I thought the people in this page help new programmers to increase there skills by getting extra input from experts like you, instead you quoted Building house, still i would expect more. If possible do that program in both way using char array and using string...

Thank you
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
vinaykumar vinay wrote:If possible do that program in both way using char array and using string...

As Henry has already said, it's not possible to do it with Strings.

vinaykumar vinay wrote:As i am new to programming world, I thought the people in this page help new programmers to increase there skills by getting extra input from experts like you, instead you quoted Building house, still i would expect more.

The building analogy was a response to Jelle's suggestion to illustrate why I thought it shouldn't be a valid solution. A useful skill for programmers is to be able to explain their code and the use of analogies can be helpful. So now you can consider your skills slightly increased.

Another useful skill is to not be so dismissive when people try to help. Otherwise you may find they don't bother trying to help you again.
vinaykumar vinay
Greenhorn

Joined: Jun 22, 2012
Posts: 9
Thank you Adrian
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

Adrian Burkett wrote:If that's the answer ...


It's not, well not a generically applicable solution anyway. It's just a loophole that would work given a specific set of restrictions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40


In my opinion, regarding the original purpose of the question, ie. to answer an interview question, then I think the current direction is fine.... meaning explain that it can't be done if no methods of strings are allowed, offer to redefine string as character array, and then talk about how you would reverse the elements of an array.

This way, you show your thinking through a problem, including seeking an alternative.

Henry
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

*** RANT WARNING ***

If I was being interviewed and asked a dumb question like this, I'd get the heck out of there. What's the point of that question anyway, to see how "smart" of a programmer you are? Doing something like that is not smart at all. Ever seen the movie "Jackass"? Well, that's pretty much where anyone who attempts to do this kind of thing at work should be, in a "Jackass" movie. That kind of attitude towards programming is just the kind of thing that keeps good developers up late at night and into the wee hours of the morning, looking through logs, scratching their heads after staring at hundreds of lines of convoluted code until they no longer had hair left to scratch off. And it's killing our profession!

When I interview people, I give them some convoluted code. Then I ask them to make it better. I ask them to fire up Eclipse or any IDE they are comfortable using and ask them to clean up some nasty code. If they can do it, and explain to me what they're doing and why they are doing it, they're probably going to get hired. If they do it by writing unit tests first, then I hire them ON THE SPOT. If they can't or if they make an even bigger mess, I thank them for their time and move on to the next resume.

If you're serious about being a professional developer and are not just in it for the paycheck, then find somebody who will ask you how you can contribute to making development easier for everybody else you are going to be working with. That's the kind of company you should try to join if you want a good, fulfilling career.

EDIT: And if you are an interviewer and you ask this kind of question, then you deserve the kind of developers you get out of it.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: write a java program without using any builtin functions(not even toString(),toCharArray(),toArray()