Thread: c style (beginner)

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    c style (beginner)

    I have this assignment:

    Write a program that reads input up to # and reports the number of times that the sequence ei occurs.
    I was wondering which method of the two below is preferred (red or green). They both tested fine, but is one better than the other? Would one be faster than the other, or is one more stylistic for programming in c? I prefer the latter, but I don't have any reason why.

    I don't have a class and it's not for a grade, I'm just curious. thanks for any input




    Code:
    #include<stdio.h>
    #include<stdbool.h>
    
    int main(void) {
        bool e = false;
        int c;
        int count = 0;
        
        printf("Enter something\n");
        
        while ((c = getchar()) != '#') {
            
            /*switch (c) {
                case 'e':
                    e = true;
                    continue;
                case 'i' :
                    if (e) 
                        count++;
                default :                
                    e = false;
            }*/
                
            
            if (c == 'e') 
                e = true;
            
            else if (c == 'i') {
                if (e)
                    count++;
                e = false;
            }
            else 
                e = false;
        }
        printf("You have %d %s of \"ei\"\n,", count,
            (count == 1) ? "grouping" : "groupings");
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not thrilled about the red one -- using continue inside the switch to continue the while loop and using fall through without a comment both are a little bothersome to me.

  3. #3
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Hmm... Check out this method.

    Code:
    // Some function
    #include <stdlib.h>
    #include <stdio.h>
    
    int Parse(char * string);
    
    int main(int argc, char * argv[])
    {
    	if (argc != 2) {
    		printf("Correct Format: &#37;s string#\n", argv[0]);
    		return 0;
    	}
    	
    	printf("There were %d occurences of 'ei'!\n", Parse(argv[1]));
    	return 0;
    }
    
    int Parse(char * string)
    {
    	int amt;
    	if (!string) return amt;
    	
    	for(amt = 0; string[0] != '#' && string[1] != '\0'; string++)
    		if (string[0] == 'e' && string[1] == 'i') ++amt;
    	return amt;
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by MeTh0Dz View Post
    Hmm... Check out this method.

    Code:
    // Some function
    #include <stdlib.h>
    #include <stdio.h>
    
    int Parse(char * string);
    
    int main(int argc, char * argv[])
    {
    	if (argc != 2) {
    		printf("Correct Format: %s string#\n", argv[0]);
    		return 0;
    	}
    	
    	printf("There were %d occurences of 'ei'!\n", Parse(argv[1]));
    	return 0;
    }
    
    int Parse(char * string)
    {
    	int amt;
    	/*if (!string) return amt;*/
    	if(string == NULL || string[0] == 0) return 0;
    	
    	for(amt = 0; string[0] != '#' && string[1] != '\0'; string++)
    		if (string[0] == 'e' && string[1] == 'i') ++amt;
    	return amt;
    }
    There are a couple of things I noticed here. First, if string is NULL, an indeterminate value is being returned because amt has not been initialized. Second, if an empty string is passed, the code reads one past the end of the string. The change I made accounts for both of these.

    Not a bug, but you'd really want to make the argument to Parse() const since Parse() does not modify the string.

  5. #5
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Good point, I was careless.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    thanks, but whew...too advanced for me at this point. Hopefully I will understand all that in a couple of months.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM