Thread: Most spaces

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Most spaces

    I want to create program which count longest series of spaces. Here is my code. I always shows 0 no matter how many spaces consist. I am only starting learning c programming so where is the problem?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        char string[20];
        int count = 0;
        int spaces=0, before=0, max=0;
        int ch;
        printf("enter string\n");
        gets(string);
    
    while (1) {
            ch = getchar();
            if (ch == EOF)
                break;
            if (ch==' ')
            {
                if (before=0)
                
                    spaces=1;
                    before=1;
                      
                if (spaces>max) max=spaces;
                spaces=0;
                before=0;
            }
          
        printf("Longest serie of spaces %d\n", max);
        while (strlen(string)!=0);
        system("PAUSE");
        return (0); 
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, SourceForge.net: Gets - cpwiki

    Second, if (before=0) lookup = vs. ==

    Third, while (strlen(string)!=0);
    When do you expect this condition to become false, if it is true to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        char string[20];
        int count = 0;
    
        int spaces=0, before=0, max=0;
    
        int ch;
    
        printf("enter string\n");
    
        gets(string);
    
     
    
    while (1) {        
    
            if (ch == EOF)
    
                break;
            if (ch==' ')
            {
    
                if (before==0)
    
                 
    
                    spaces=1;
    
                    before=1;
    
                       
    
                if (spaces>max) max=spaces;
    
                spaces=0;
    
                before=0;
    
            }
    
           
    
        printf("Longest serie of spaces %d\n", max);    
    
        system("PAUSE");
    
        return (0); 
    
    }
    
    }
    Now it looks like this but still output is always 0

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This code has some problems:

    Code:
                if (before==0)
    
                 
    
                    spaces=1;  //<<== is subordinate to if()
    
                    before=1;   //<<==is NOT subordinate to if()
    
                       
    
                if (spaces>max) max=spaces;
    
                spaces=0;
    
                before=0;
    the spaces and before assignment lines of code, following the upper if statement, appear through the indentation, to be equal in level of subordination - and they are not equal. Only spaces=1 is suborordinate and will be executed when the if() statement tests out as true.

    That's why indentation in code is very important.

    I'm not saying this logic is any better than yours, but intuitively, it seems easiest to just use another nested while() loop:
    Code:
         space=0;       //prime the while pump
         while(char is a space) 
             spaces++;
         if(spaces > maxSpaces)
             maxSpaces=spaces;
    Tightly coupled logic like that while loop ^^^ is easy to get right, imo.

    I wonder how many people in America today, have ever pumped water from a hand pump well, that had to be primed?

    Not many I'd guess, but while() loops frequently need some "priming" to set up the variable values it will use.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Maybe I ask to much but maybe you can write how it have to look because I still cannot get a right result then I will have an example for the future.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need the "before" variable or logic based on it. Just step through the string, char by char, from the first char, to the last one. When you hit a space, let the while loop I showed you above, do it's work, counting the spaces for as long as they go on.

    When it hits a non-space letter, then the while loop I showed you will quit, and the outer while loop will start working again:

    Code:
    while(there is another letter) {  //the outer while loop you have
    
       while(the letter is a space)  {  //the inner while loop I showed you
             //code to count the spaces
        }
        if(spaces > max_spaces)
            assign spaces to max_spaces
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        char string[20];
        int count = 0;
        int spaces=0, max=0;
        int ch;
        printf("enter string\n");
        gets(string);
     
    while (1) {
            
            if (ch == EOF)
                break;
            {
                spaces=0; 
                }      
    while (ch==' ') ;
          }        
        spaces++;
    if(spaces > max);
        max=spaces;
            
        printf("Longest serie of spaces %d\n", max);
        
        system("PAUSE");
        return (0);
    }
    Now my code looks like this but i enter the string and don/t get any output. So what i'm doing wrong now?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Nothing ever sets or changes ch.

    When do you expect this condition to become false, if it is true to begin with:
    Code:
    while (ch==' ') ;
    If you intend to examine all the characters in 'string', then EOF is of no use to you. Use array indexing, i.e. string[i]

    Also, never use gets. That function is almost unanimously regarded as a mistake. It is impossible to prevent buffer overrun when using it. In your case if you input a string longer than 19 characters then it will corrupt memory. Use fgets instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Can you fix this code? Because i working on it several days then i will have example for the future

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    anyone?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your code, indented to make it reflect what it is actually doing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        char string[20];
        int count = 0;
        int spaces=0, max=0;
        int ch;
    
        printf("enter string\n");
        gets(string);
    
        while (1) {
            if (ch == EOF)
            break;
            {
                spaces=0; 
            }      
            while (ch==' ') ;
        }        
    
        spaces++;
    
        if(spaces > max);
    
        max=spaces;
        
        printf("Longest serie of spaces %d\n", max);
        
        system("PAUSE");
        return (0);
    }
    Now go through it one line at a time, with your C book/tutorial by your side, and figure out what exactly this code is doing at the moment.

    When you understand why it is doing what it is doing now, perhaps you might be able to write a program which does what you want.

    As opposed to say at the moment, writing anything which compiles, and then asking us to basically rewrite it to do what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12

  13. #13
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        char string[100];
        int count = 0;
        int max=0;
    
        printf("enter string\n");
        gets(string);
     int i=0;
    
    while (string[i+1]!='\0') {
         if(string[i+1]==' ')++count;
         if((string[i]=' ' && string[i+1]!=' ')&& count>max)max=count,count=0;
         ++i;
    }
        printf("Longest serie of spaces %d\n", max);
        while (strlen(string)!=0);
        system("PAUSE");
        return (0);
    
    }

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (string[i+1]!='\0')
    And if the user just presses enter, what part of the string is being read by i+1 ?

    Did you spot the use of = rather than == as well?

    And read post #2 again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by Salem View Post


    Did you spot the use of = rather than == as well?

    And read post #2 again.
    sorry it is my mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get rid of spaces
    By wasi.cjr in forum C Programming
    Replies: 5
    Last Post: 03-21-2011, 05:31 PM
  2. spaces
    By olds-kool in forum C Programming
    Replies: 3
    Last Post: 08-13-2010, 11:56 AM
  3. How do I allow spaces on a cin?
    By boblablabla in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2009, 02:05 PM
  4. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  5. set spaces in Tab.
    By nilrac in forum C Programming
    Replies: 8
    Last Post: 11-14-2002, 01:58 PM