Sunday, 24 June 2012

Write a function which returns a length of a given string


Algorithm: find the last character and return the distance between it and the first one.

We can use helper pointer:



Alternatively, we can use just index:

main.cpp:


Output:
String: ""
Length: 0
String: "a"
Length: 1
String: "ab"
Length: 2
String: "abc"
Length: 3
String: "abcdefg"
Length: 7

No comments:

Post a Comment