Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3286606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:23:57+00:00 2026-05-17T20:23:57+00:00

I am a bit new to Objective C and was wondering if there is

  • 0

I am a bit new to Objective C and was wondering if there is a better way to count words in a string.

ie:

NSString *str = @"this is a string";

// return should be 4 words ..

The way I now how to do it is by breaking the string into an array of words space (‘ ‘) character and count the array.

Any advise will be appreciated! Thanks!! 🙂

EDIT:
For those of you who came here looking for answer; I found a similar post with an excellent reply.

How to count words within a text string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:23:58+00:00Added an answer on May 17, 2026 at 8:23 pm

    Unless you’re going to be doing it hundreds of times a second, I would just opt for the readable solution, something like the following pseudocode:

    def count (str):
        lastchar = " "
        count = 0
        for char as every character in string:
            if char is not whitespace and lastchar is whitespace:
                count = count + 1
            lastchar = char
        return count
    

    It seems a bit of a waste to create a whole array of other strings just so you can count them and throw them away.

    And if, for some reason, it becomes an issue, you can just replace the function body with a faster version. Make sure it is a problem first however. Optimisation of code that’s fast enough already is wasted effort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I am a bit new to iOS Development and I was wondering if someone
Very new to Objective-C and Xcode and working my way through modifying the DrillDownApp
I am new to Objective-C and a bit rusty at using C++ and templates,
I'm new to Objective-C, so I may be phrasing this incorrectly. I've created a
I'm a bit new to objective-c and even newer at programming with Quartz 2D,
Apologies if my terminology is a bit off, new to objective C! I am
I am trying to write a bit of Objective-C (which is new for me,
I've been stumped with this for the last few days, I'm a bit new
I am new to objective C so I am bit confused on how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.