Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Surinder Singh
Surinder Singh
274 Points

Swift COde : test a function that takes a String as a parameter & returns back the number of characters and nmbr of wrds

how to solve this using swift programming???

2 Answers

Thomas Dobson
Thomas Dobson
7,511 Points

Hi Surinder,

try something like this

func countString (byNumberOfCharactures count: String) -> Int {
    let myString = count
    let countMyString = myString.characters.count

    return countMyString

countString(byNumberOfCharactures: "This is a Duck")
//returns 14

I didn't know this either, but as always there is lots of good documentation provided by apple.

Here is where I found this information: Swift Standard Library: Strings and text

As for counting words... there are a number or more complicated ways to do this; they can be easily found with a quick google search. I'd e recommend sticking to the basics for now :).

I hope this helps