Solved

Count the number of space character in a string

Posted on 2007-04-02
19
516 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
  • 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
 
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
Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now