-2
String str="This is interesting";

How can I find the length of the string ?

Conditions:

1) Not to use any String methods

RohitWagh
  • 1,999
  • 3
  • 22
  • 43
Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
  • 4
    Count with your fingers? – Mat Mar 10 '12 at 14:50
  • 4
    Maybe it's me, but I don't see any educational value in such questions – M Platvoet Mar 10 '12 at 14:52
  • Write a loop that counts the letters. – alexis Mar 10 '12 at 14:55
  • Did you ever read your class book? If you could have see so many examples for this purpose. – kosa Mar 10 '12 at 14:56
  • @alexis -- but how do you terminate the loop? – Hot Licks Mar 10 '12 at 14:56
  • This is the first time am posting under homework tag. I was asked this question in an interview. Thats why I posted it under homework. I have tried, dint find any solution, so asked here. Upto you all if to answer or close it – Anuj Balan Mar 10 '12 at 14:57
  • 1
    possible duplicate of [String length without using length() method in java](http://stackoverflow.com/questions/2910336/string-length-without-using-length-method-in-java). Also read [this](http://stackoverflow.com/questions/4133701/string-length-in-twips-java)... Its interesting... – Fahim Parkar Mar 10 '12 at 14:58
  • Who knows? It's a contrived program, and it's homework. I'm just giving him something to think about. He could use an iterator, if they've learned them already. – alexis Mar 10 '12 at 14:59
  • @MPlatvoet -- Maybe the "educational value" is that some programming problems don't have a good answer -- usually because the wrong question is being asked. – Hot Licks Mar 10 '12 at 15:00
  • Thanks Fahim. This link looks helpful – Anuj Balan Mar 10 '12 at 15:00
  • `Int32 count = 0; String target = "Foo"; foreach (Char item in target) { count++;} //Display count` This isn't java, this is c# code. But basically idea is the same. Use foreach loop. – Oybek Mar 10 '12 at 15:03
  • And honestly, the interviewer is stupid. If I was asked a such question I'd humilate the asker with a loud laughter. – Oybek Mar 10 '12 at 15:05
  • Even I wanted to laugh. It was a stupid interview on the whole. They were rather happy asking puzzles than testing the conceptual knowledge – Anuj Balan Mar 10 '12 at 15:13
  • Ah, interview question, that's different! The C approach: Find out how strings are represented, and read off the length bytes (it's not a C string) via pointer arithmetic. The python approach: Iterate over the string and count the characters. The java approach: Cast it to something whose methods you *are* allowed to use? The google approach: look on the web for more solutions. It's a lousy question in terms of "educational value", but I can see how it could be revealing in a job interview. (I'd have probably answered "What could possibly be the point of that?" :-) – alexis Mar 10 '12 at 15:15
  • The questions asked never had anything to do with testing the programming skills. All were silly and gets your confidence degraded. Ended up frustrated the whole day and thought of finding the answers. – Anuj Balan Mar 10 '12 at 15:26
  • 2
    `StringBuilder sb = new StringBuilder(string); return sb.length();` – Daniel Fischer Mar 10 '12 at 17:08

2 Answers2

2

If you can't use any String methods to find the length of the String, you can use reflection (Hint: String#length returns the field count). However, seeing as this is homework, I think you may have misunderstood the question you're being asked.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I haven't been understanding it differently. I am meant not to use any methods in String. They want us to think differently and find out. Using reflections ?? How? – Anuj Balan Mar 10 '12 at 14:51
  • @Ajj I linked you to the reflection tutorial. This is homework, so I won't do it for you. You probably want to take a look at the [Member](http://docs.oracle.com/javase/tutorial/reflect/member/index.html) section of that tutorial. – Jeffrey Mar 10 '12 at 14:53
0

is str.length(); still not allowed?

OR New Link (StringLength.java)

John Woo
  • 258,903
  • 69
  • 498
  • 492