Solved

Count the number of space character in a string

Posted on 2007-04-02
19
515 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
yes,i'm sure there is line line[length-1]='\0'.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Were you able to fix it ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
And '\0' must be added by compiler or not?
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
>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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
OK, I understood your post, it was because of  line[length-1]='\0';
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
> it's 3am here
Not by me :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 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

8 Experts available now in Live!

Get 1:1 Help Now