-7

I am using the following code it is counting the spaces in the string but i don't want to count spaces.

#include<stdio.h>
#include<conio.h>

void main()
{
   char*ptr,str[30];
   int size=0;
   int word=0;

   puts("enter the string");
   gets(str);
   fflush(stdin);

   for(ptr=str;*ptr!='\0',ptr++)
   {
       size++;
   }

   printf("size of string is = %d",size);
   getchar();
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
shweta
  • 1
  • Is this how you write your code? No new lines, no spaces, nothing? – Kiril Kirov Jun 10 '15 at 07:24
  • 6
    how is this python? – Sourav Ghosh Jun 10 '15 at 07:25
  • 1
    `void main` is not standard c. Are you using a non-standard compiler? – eerorika Jun 10 '15 at 07:26
  • 2
    Rewrite `size++;` to `if(some_condition_to_check_if_ptr_is_not_space) size++;`. Now it is up to you find what `some_condition_to_check_if_ptr_is_not_space` is? – Mohit Jain Jun 10 '15 at 07:26
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Eregrith Jun 10 '15 at 07:27
  • @MohitJain is not that obvious? – VP. Jun 10 '15 at 07:27
  • Not related to your problem, but NEVER EVER USE gets, IT IS DANGEROUSLY UNSAFE. Use fgets(str, sizeof(str), stdin) instead. And drop that fflush(stdin), it is undefined behavior (https://stackoverflow.com/a/2979217/311635). – Rudi Jun 10 '15 at 07:33

4 Answers4

5

TL;DR; answer : Skip the counter (size) increment, when the value (at that pointer location) is a space.

Please write the code yourself and update us about the result.

That said, IMHO, first you should notice (and correct) a few things, as

  1. void main() is not standard. Use int main(void) instead.
  2. Never use gets().It suffers seriously from buffer overflow issues. Use fgets() instead.
  3. fflush(stdin) is undefined behaviour. Get rid of it ASAP.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Try the following codes using pointer arithmetics instead:

while (*ptr) {
    if (*ptr != ' ') {
        size++;
    }
}

This will loop through the characters until it hit a null terminator '\0', and increment as necessary.

Arial
  • 4,844
  • 2
  • 18
  • 17
  • 3
    this is rather overkill for the problem at hand. Given he already has a loop that iterates all characters. Much simpler to just skip spaces inside the counting loop. – DrKoch Jun 10 '15 at 07:31
  • Very good point. Looping over the characters and ignoring the space is a much efficient way to solve this problem and gives O(n). I'll change my answer. :) – Arial Jun 10 '15 at 07:45
0

Try this

int my_strlen(char *str)
{
int i, j = 0;
while (str[i] != '\0')
{
   if (str[i] != ' ')
       ++j;
   ++i;
}
return j;
}
Guillaume Munsch
  • 1,233
  • 17
  • 37
  • 3
    Another spoiler. Keeps OP from learning. Fortunately your code has a bug, so OP needs to learn even more - good. – DrKoch Jun 10 '15 at 07:32
0

Inside your loop:

skip spaces

count all other characters

DrKoch
  • 9,556
  • 2
  • 34
  • 43