This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vinaykumar vinay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean without using any of the methods of the String class or without using any methods of any class ?
 
vinaykumar vinay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes without using any of the java built in functions.... not even string functions also....
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vinaykumar vinay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Adrian
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*** 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.
 
koushik reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
best and simple method for string reply without using inbuilt functions....
word = scanner.nextLine();
String rev = "";
for(int i=word.length()-1;i>=0;i--){
rev = rev + word.charAt(i);
}
System.out.println("Reverse is = "+rev);
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
koushik reddy wrote:best and simple method for string reply without using inbuilt functions....
rev = rev + word.charAt(i);
System.out.println("Reverse is = "+rev);


What does "inbuilt function" mean? Why are charAt and println not among them?
 
koushik reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
those are basic functions and i mean that i didnt used any toString(),toCharArray(),toArray() methods.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same arguments could (mis)lead us to use the reverse() method of StringBuilder.
It is no toString(), toCharArray(), toArray() either.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinaykumar vinay wrote:As i am new to programming world, I thought the people in this page help new programmers to increase there skills...


Yes, that's so. But as an experienced programmer, let me tell you that being able to answer this question will not improve your skills in the slightest. It's possible that you might be able to find some horribly obscure way of answering the question, but then all you have learned is a bad habit. You should never write horribly obscure code when simple straightforward code can be written.
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

If you can simply change the requirements so that you can use char arrays - then simply change the requirements so that this will only work with palindromic strings.

Program becomes trivial and it uses no explicit built-in function.

-steve
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
koushik reddy wrote:best and simple method for string reply without using inbuilt functions....
word = scanner.nextLine();
String rev = "";
for(int i=word.length()-1;i>=0;i--){
rev = rev + word.charAt(i);
}
System.out.println("Reverse is = "+rev);

Without using any built-in functions?

According to me you are using lots of built-in functions here! You are using Scanner.nextLine, String.length(), String.charAt() and PrintStream.println().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic