• C

Count the number of space character in a string

How do i count the number of space in a string using C?I tried but it did not work
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
  char line[255];
  int ;
  int n=0,length,m;
  for(;;)
  {
  printf("enter command: ");
 
fgets(line,sizeof(line),stdin);

  length=strlen(line);

  line[length-1]='\0';

    if(strcmp(line,"quit")==0)
  {
            break;
  }
  else
  {
       for(m=0;m<length-1;m++)
      {
 //compare each character in string "line" with the ASCII code of Space    
 if(line[m]==32)
       {
                     n++;              
       }
       printf("%c\n",line[m]);
      }
           
  }          
 }
        
  system("PAUSE");
  return 0;
}
I replace other ASCII code and it worked except the ASCII code of Space
LVL 2
khoamaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
It looks good, except that you don't show the result. Add this after the innermost for loop :

    printf("counted %d spaces\n", n);


Also, you should reset n at the beginning of the outermost for loop.
0
AlexNekCommented:
I can't see something wrong, except some little problems

for(m=0; m < length-1;m++)
I would use
for(m=0; m < length; m++)

You not write n value

Pay attention that you can see only new line in your test output.
Use instead
printf("%c\n",line[m]);

printf("%d\n",line[m]);
0
Infinity08Commented:
AlexNek,

>> for(m=0; m < length-1;m++)

is correct : the character at length-1 is the NULL character :

>> line[length-1]='\0';
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

AlexNekCommented:
>is correct : the character at length-1 is the NULL character
Are you sure that strlen consider the '\0' character?
http://www.cplusplus.com/reference/clibrary/cstring/strlen.html

I not see that line. It can be wrong too.
line[length-1]='\0';
0
khoamaAuthor Commented:
i found the problem. I type the string " ls -1 a" as input and the result is (i changed printf("%c\n",line[m]);)
108
115
0
49
32
97
Please notice the number 0 i dont know why it didnt understand the first space?It just understand the second space.Its ASCII code is correct (32).It's so weired.I also tried to print the value of n before and it's 1 (because just have one space).
Thanks for your comments
0
khoamaAuthor Commented:
yes,i'm sure there is line line[length-1]='\0'.
0
Infinity08Commented:
AlexNek,

>> Are you sure that strlen consider the '\0' character?

Well, read the page you linked to - it says :

        "The length of a C string is determined by the terminating null-character"

Seems pretty clear to me :)


>> I not see that line. It can be wrong too.

What do you mean ? The line is right there in his code - it's to erase the trailing \n from the input.



khoama,

>> i found the problem.

That should not happen with the code you posted - Did you show the complete code ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khoamaAuthor Commented:
Sorry guys i'm really stupid because i use strtok before the for loop so it caused this problem.Once again , thank you very much
0
Infinity08Commented:
Were you able to fix it ?
0
Infinity08Commented:
>> because i use strtok before the for loop so it caused this problem

That would explain it heh :)
0
AlexNekCommented:
2 Infinity08
But it written too
A C string is as long as the amount of characters between the beginning of the string and the terminating null character
Could you tell me why m is 7 here?
int m = strlen("1234567");
0
Infinity08Commented:
>> Could you tell me why m is 7 here?

Well, there are 7 characters in the string : '1', '2', '3', '4', '5', '6' and '7'
0
AlexNekCommented:
And '\0' must be added by compiler or not?
0
Infinity08Commented:
>> And '\0' must be added by compiler or not?

The '\0' is there after the 7 characters. strlen() will count upto, but not including the '\0' character.
0
AlexNekCommented:
>The '\0' is there after the 7 characters. strlen() will count upto, but not including the '\0' character.
that is exactly what I want to tell.
0
khoamaAuthor Commented:
Hi guys
m=7 is correct because the strlen function does not include '\0' terminator.It gives you the exact length of the string.You can read it here
http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/strlen.html
G9 all, it's 3am here :)
0
Infinity08Commented:
>> that is exactly what I want to tell.

Then what's your point ? I'm afraid you've lost me ...
0
AlexNekCommented:
OK, I understood your post, it was because of  line[length-1]='\0';
0
AlexNekCommented:
> it's 3am here
Not by me :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.