Question:
Write a program to find the length of a string without inbuilt function
Program:
1 2 3 4 5 6 |
def strlen(s): if s == "": return 0 return s.rindex(s[-1]) + 1 s= "GlobalSQA" print(strlen(s)) |
Explanation:
The strlen function return 0 if the string is empty. Otherwise it adds one to the index position of it;s last element obtained by s.rindex.
Output:
1 |
9 |
Leave a Reply