Solved

Count the number of space character in a string

Posted on 2007-04-02
19
524 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:khoama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 4
19 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18838658
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
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 200 total points
ID: 18838713
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18838732
AlexNek,

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

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

>> line[length-1]='\0';
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:AlexNek
ID: 18838822
>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
 
LVL 2

Author Comment

by:khoama
ID: 18838824
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
 
LVL 2

Author Comment

by:khoama
ID: 18838838
yes,i'm sure there is line line[length-1]='\0'.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 300 total points
ID: 18838855
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
 
LVL 2

Author Comment

by:khoama
ID: 18838896
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18838902
Were you able to fix it ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18838905
>> because i use strtok before the for loop so it caused this problem

That would explain it heh :)
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18838942
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18838960
>> 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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18838992
And '\0' must be added by compiler or not?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18839004
>> 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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18839013
>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
 
LVL 2

Author Comment

by:khoama
ID: 18839022
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18839026
>> that is exactly what I want to tell.

Then what's your point ? I'm afraid you've lost me ...
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18839029
OK, I understood your post, it was because of  line[length-1]='\0';
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18839036
> it's 3am here
Not by me :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question