0

Possible Duplicate:
String length without using length() method in java

I need to get the length of a string (no of characters present) in Java using a for loop and without using any methods like length().

import java.io.*;
import java.util.*;

public class reversestring

{
    public static void main(String arg[])throws IOException

    {
     String s;
     int i=0,j=0,k=0;
     DataInputStream in=new DataInputStream(System.in);
     System.out.println("Enter ur string : ");
     s=in.readLine();
     char ar[]=s.toCharArray();
     System.out.println("Length of the string is : ");

     for(j=ar[i];j!='\0';i++)
     {

         k++;

     }
     System.out.println(+k);
    }
}

I wrote this program, but I am not getting the answer. What is wrong with it?

Community
  • 1
  • 1
  • 1
    Java `String` objects are **not** 0-terminated! – Joachim Sauer Feb 10 '11 at 14:42
  • 1
    As you "cannot" use `length()`.. should this be tagged "homework" ? – Nanne Feb 10 '11 at 14:44
  • 1
    @Nanne: according to [this](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions) the homework-tag (together with other meta-tags) is discouraged. – Joachim Sauer Feb 10 '11 at 14:47
  • 1
    Possible duplicate: http://stackoverflow.com/questions/2910336/string-length-without-using-length-method-in-java/2910357#2910357 – aioobe Feb 10 '11 at 14:49
  • Ha, ha, ha, ha ... ! Sorry, sorry, thousand apologies. Could not resist the humour. – Blessed Geek Feb 10 '11 at 14:49
  • Not a real question. You check the length of a String in Java using the length() method, and there is no defensible reason to do it any other way. – Christoffer Hammarström Feb 10 '11 at 14:51
  • 3
    @joachim-sauer : That's not what I read in that link; it's about how to _respond_ to those questions. I was suggesting it, because it would help understanding the question: otherwise I'd ask to specify why `length()` couldn't be used. If there is some strange reason for that, we might need to take that into account too. But as it is probably homework, we can just take it as a given. So it's not "becasue I don't want to answer that!" but more "what is going on?". See other comments :D – Nanne Feb 10 '11 at 14:51

7 Answers7

6

This has been asked before. Here's my favorite answer:

str.lastIndexOf("")

(which probably even runs in constant time, as opposed to the other answers.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
5

The following .length is not a method.

int length = s.toCharArray().length
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Java isn't C, thus you can't treat Java strings as C strings and you can't expect C methods to work in Java. In particular, Java strings aren't null-terminated.

The 'correct' way would be to use length (either a string method, or an array property), but, since you don't want, you could employ 'for each' loop.

for (char c in charArray) {
    ++count;
}

It feels not good, though.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • The task itself is completely meaningless in the first place, which makes it pretty impossible to give a "good" answer. Having said that: this only hides the `.length` behind some syntactic sugar ;-) – Joachim Sauer Feb 10 '11 at 14:44
  • @Joachim I completely agree, I would never let this code pass review, let alone use it myself :) – Nikita Rybak Feb 10 '11 at 14:45
1

a functional guy would answer

len(str)
   return str.isEmpty() ? 0 : 1+len(str.substring(1));
irreputable
  • 44,725
  • 9
  • 65
  • 93
1
int counter;
String s = "something";

try{
    for(counter=0; s.charAt(counter); counter++);       

}catch(Exception e){
   //ArrayIndexOutOfBoundsException
   System.out.println("Length: " + counter);
}
David
  • 21
  • 1
0

I believe Java's String.toCharArray() returns an array that is not null-terminated, so looking for a null character would not work.

irritate
  • 7,350
  • 1
  • 17
  • 11
0

I can't believe all the other answers are using the String API, although the title of this question indicates that's not allowed :-P

I came up with this:

System.out.println(new StringReader("stackoverflow").skip(Long.MAX_VALUE));
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • I strongly suspect that StringReader is using the String API behind the scenes. You are simply delegating the use of the API to it. – ILMTitan Feb 10 '11 at 16:50