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

  • SEARCH
  • Home
  • 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 1001741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:43:25+00:00 2026-05-16T07:43:25+00:00

I am trying to create a small function that returns the number of spaces

  • 0

I am trying to create a small function that returns the number of spaces in a Char*
variable using the C language.

Let’s say I have this string :"hello hello hello". I want the function to return 2.
This is the code I have so far:

int blankcounter(char* pline)
{                              
  int i=0;                  
  int counter = 0;            
  while (pline[i] != '\0')       
  {                               
    if (pline[i++] ==' ')
      counter++;
  }
  return counter;
}

the source that i am reading from is a txt file and 1 correction that i have to add is that the code that i posted indeed works but got 1 downside: if for example i want to read : “hello whats up “i want my function to be able to return 2 but it returns 3 because of the space that appears just after the wordup do u have any suggestion for me so it will return 2 ?

  • 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-16T07:43:26+00:00Added an answer on May 16, 2026 at 7:43 am

    I suspect you would not want to count leading spaces too ” hello whats up” should return 2 as well right?

    I can propose 2 solutions. First is to trim all spaces before you start counting them How do I trim leading/trailing whitespace in a standard way?.

    The second is to adjust the start and end point of your loop to the first and last non-space characters and then perform the count:

    int start=0;  
    int end = strlen(pline);  
    int i=0;  
    while (pline[i++]==' ') start++;  
    i=end-1;  
    while (pline[i--]==' '&& i >= 0) end--;  
    for (i=start; i<end; i++) { your count procedure} 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small program that I'm trying to create to get ip addresses
I am trying to create a small web app that will allow users to
I have a small assignment in C. I am trying to create an array
I'm trying to create a custom patch for Quartz Composer that will function just
I am trying create a small web application that allows a user to login
I have a small function below that accepts associative arrays and I'm struggling to
I'm trying to create a small Javascript framework that I can use in my
I'm trying to create a small form that submits a form and passes a
I am trying to create a porthole using CSS. When I say porthole, what
I'm trying to create a window that will become a small popup window, containing

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.