Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

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