1

Question was asked to find the length of a String in Java without using any inbuilt functions like length(), toCharArray()? My approach was to somehow convert it into charArray so that we can itterate and find the length.

I searched but in most of the places charAt() is used which is again a inbuilt function.

I checked how java does it. String class have a count variable which stores length of the string. But it is private and final. So we cant access it outside String class. For storing string, String class uses a character array, which is also private and final. So we cant use this array also to find the length.

So need to know how can we find it? or Is it possible also to find the length without using any inbuilt functions?

user1425349
  • 71
  • 1
  • 6
  • yes possible ,, best of luck – stinepike Mar 09 '16 at 04:21
  • 1
    Possible duplicate of [String length without using length() method](http://stackoverflow.com/questions/2910336/string-length-without-using-length-method) – seenukarthi Mar 09 '16 at 04:21
  • Not sure this is possible using _no_ inbuilt functions. There are ways to avoid using length(), but if you actually forbid the use of any library functions you literally can't do anything with that string. You could do something evil with the JNI, I suppose, but I doubt OP knows what that is. – Chris Kitching Mar 09 '16 at 04:22
  • What is the problem with built-in methods? – mmuzahid Mar 09 '16 at 04:33
  • 1
    @KarthikeyanVaithilingam I was wondering the same thing, but my guess is that some interviewer would rather show off how much obscure knowledge he knows than find the right person to fill the position. – ajb Mar 09 '16 at 04:45
  • By no "inbuilt functions", do you mean no functions that would give you the length more or less directly, or literally _any_ function? – Maljam Mar 09 '16 at 05:23
  • Can you use StringBuilder's inbuild functions? – Eva Mar 09 '16 at 08:13

3 Answers3

2

You can access the count variable in the String class by setting the count variable accessibility as true though it may through security exceptions sometimes. For instance :

Field var = String.class.getDeclaredField("count"); var.setAccessible(true); String str = "variable"; int count = var.getInt(str);

Reference : http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/AccessibleObject.html

Rishi
  • 1,163
  • 7
  • 12
1

It is definitely possible. Two sources I found useful were javahungry and codenirvana.

Also, it seems this question has been asked before. Please reference String length without using length() method

EDIT: Apologies, the javahungry and codenirvana links used one of these methods that were to be excluded. Following the solution by aioobe in the above stackoverflow link, I found the following to work (assuming you can at least use functions outside of String class).

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringLength { String testString;

public StringLength() {
    testString = "I Love Boats"; // Place your string here
    System.out.println("Length of String is " + getStringLength(testString));
}

// Implemented using https://stackoverflow.com/questions/2910336/string-length-without-using-length-method?lq=1
// solution originally proposed by user aioobe
public int getStringLength(String s) {
    int strLength = 0;

    Matcher m = Pattern.compile("$").matcher(testString);
    m.find();
    strLength = m.end();

    return strLength;
}

public static void main(String Args[]) {
    new StringLength();
}

}

Community
  • 1
  • 1
Eidan
  • 11
  • 4
  • 1
    Since both use `charAt()` which is an inbuilt function of String, they are both false advertising. – user949300 Mar 09 '16 at 05:16
  • I revised my answer, thank you very much for pointing that out to me. I forgot to check for that method call. – Eidan Mar 09 '16 at 05:33
  • This works..Thanks a lot. Also can we reverse a string without using inbuilt function? – user1425349 Mar 10 '16 at 19:41
  • First we need to figure out how to split. I've seen it done using the StringTokenizer api, if you are permitted to use that. Feed the length of string from the above code snippet into an int value instead of calling length() in the for loop from the first bit of code found on this link: [devxTip](http://www.devx.com/tips/Tip/42787). I'll try this myself once I'm back at a pc. After splitting into like a char array or something, we can traverse the array backwards and spit out that string. – Eidan Mar 10 '16 at 20:09
1

I'll quote the best, most absurd answer from the linked SO question. This one only uses constructors and equals(), so arguably it doesn't use any methods of String. All credit should go to the original answerer.

"Just to complete this with the most stupid method I can come up with: Generate all possible strings of length 1, use equals to compare them to the original string; if they are equal, the string length is 1. If no string matches, generate all possible strings of length 2, compare them, for string length 2. Etc. Continue until you find the string length or the universe ends, whatever happens first."

user949300
  • 15,364
  • 7
  • 35
  • 66