views:

175

answers:

9

hi can anyone tell me how can i get the length of a string if a user input it and we want to know the length of the string without sing len function or any string methods please anyone tell me as i m tapping me head madly for the answer..... thank you

+3  A: 

Why you need to avoid the len function is beyond me, but strings are iterables. You should be able to do this:

strlen = 0

for c in myString:
  strlen += 1
g.d.d.c
+1  A: 

easy:

length=0
for x in "This is a string":
    length+=1
print(length)
Gary
hey thats too cooll but can u tell me how this is working in a string range bcause we really dont know the range and in a for loop how can this be interprate as a integer
sam
Python strings are basically a list of characters. So in this code, it's looping through each character in the string and adding 1 to the counter (length in this case). If you want to see how it works, add print(x) right before the length+=1 line.
offsound
+9  A: 
>>> sum(map(lambda x:1, "hello world"))
11

>>> sum(1 for x in "foobar")
6

>>> from itertools import count
>>> zip(count(1), "baz")[-1][0]
3

some recursive solutions

>>> def get_string_length(s):
...     return 1 + get_string_length(s[1:]) if s else 0
... 
>>> get_string_length("hello world")
11
>>> def get_string_length_gen(s):
...     yield 1 + next(get_string_length_gen(s[1:])) if s else 0
... 
>>> next(get_string_length_gen("hello world"))
11
>>> 
gnibbler
While maybe not the best answer, this wins "coolest answer".
Nate C-K
`get_string_length(s)` seems reminiscent of Haskell...
detly
+1  A: 

It's a weird question so here's a weird answer!

try:
  for i in itertools.count(): mystring[i]
except IndexError:
  pass
Ken
+1  A: 
>>> import re
>>> s
'mylongstring'
>>> re.subn(".","1",s)[-1]
12
>>>

If string contains new lines

>>> s="mys\ntring\n"
>>> re.compile(".",re.DOTALL).subn("",s)[-1]
10
ghostdog74
Doesn't work if string contains a newline! You need the re.DOTANY flag.
John Machin
A: 

Not very efficient but very concise:

def string_length(s):    
    if s == '': return 0
    return 1 + string_length(s[1:])
Nate
Variant of @gnibbler's magnum opus.
John Machin
+3  A: 

Here's an O(1) method:

def strlen(s):
    if s == "": return 0
    return s.rindex(s[-1]) + 1

In other words, it doesn't work by counting the characters, so should be just as fast for a 1GB string as it is for a 1 byte string.

It works by looking at the last character and searching from the very end to find that character. Since it's the last character it will always find it at the first place it looks, essentially always returning the index of the last character. The length is just one more than the index of the last character.

Gabe
`rindex` is a `str` method --- BBBZZZZZZZTTTTT
John Machin
John Machin: What's wrong with `str` methods? The OP only said not to use `string` methods.
Gabe
A: 

Here's a way to do it by counting the number of occurences of the empty string within the string:

def strlen(s):
    return s.count('') - 1

Since "".count("") returns 1, you have to subtract 1 to get the string's length.

Gabe
`count` is a `str` method --- BBBZZZZZZZTTTTT
John Machin
John Machin: Surely every other solution implicitly calls *some* method of `str`. The assignment only says not to use `string` methods.
Gabe
A: 

Make a file-like object from the string, read the entire object, then tell your offset:

>>> import StringIO
>>> ss = StringIO.StringIO("ABCDEFGHIJ")
>>> ss.read()
'ABCDEFGHIJ'
>>> ss.tell()
10
Paul McGuire