0

Trying to find the length of the string without using the length method…using "charAt()" function ..Kindly let know what is the last character of the string ..tried using ” and null and ‘\0′ ..but still not able to find the end character..

str=”test this string”;
int i=0;
char p=str.charAt(i);//’\u00A00′;
while(p!=’\0’){

    p=str.charAt(i);++i;

}
System.out.println("Length of string : "+str+" is "+i);
4
  • Why are you doing this? You should use the length method to find the length of a string. Commented Sep 22, 2020 at 12:12
  • 1
    Strings in java are not C-strings. They're not null-terminated sequences of characters. Commented Sep 22, 2020 at 12:15
  • 1
    String#length defines the length of the string. Java isn't C, nor should it be. I assume that you're a c programmer and are trying to apply the rules of your little village to Java. Please don't do that. Java and C, despite surface similarities, are very different villages. Commented Sep 22, 2020 at 12:21
  • I'm doing this as it is one of the interview questions... I found out that we can find the length of string without using "length" method ...using lastIndexOf and toCharArray methods ... So just wanted to know if we can do this using CharAt as well ....wanted to know how to get the end of the string character Commented Sep 22, 2020 at 12:58

1 Answer 1

2

In some (usually in C) implementations of strings, the 'length' of a string is tracked by way of a sentinel character, generally: '\0'. The contract is:

  • The actual character '\0' cannot be in the string.
  • The '\0' char is the terminator; it is not itself part of the string and signals the end.

This is not how java works, at all, though.

There are other ways to encode string length. For example, first, you store the length of the string, then you store that many bytes. In bytes, imagine this is in memory:

many bytes ... 0x00 0x00 0x00 0x05 0x48 0x65 0x6C 0x6C 0x6F ... many more bytes

Then, given a pointer to that first 0x00, code can figure out that is "Hello". You have a hypothetical way of storing strings that works just great. However, scanning for 'the ending 0x00' is not going to work because this particular way of storing strings doesn't have that.

The above is close to how java works. However, as further complication, java is java. java does not do pointers. It is not possible to get a memory pointer pointing to that length field.

Conclusion: What you want to do? Not possible. There is no \0 to find, and you can't read the memory containing the length. It's abstracted away; if you want length, you invoke stringInstance.length(). End of story.

There are of course, creative, silly, and entirely academic ways to do it. For example:

public int getStringLengthInAStupidWay(String in) {
  int i = -1;
  try {
    while (true) input.charAt(++i);
  } catch (IndexOutOfBoundsException inevitable) {}
  return i;
}

You can even make that O(logn) with some fancy maths. I have absolutely no idea what possible point there'd be to this, though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.