Thread: int StrSpn(char *str, int ch);

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    int StrSpn(char *str, int ch);

    I need to implement this function:

    Code:
    int StrSpn(char *str, int ch);
    The function StrSpn counts the number of consecutive characters in the first argument str (which is null-terminated) that consist entirely of character specified in the second argument string set (which is also null-terminated). Counting begins with the first character in the string.

    This is what it is supposed to do:

    Code:
    char *teststr = "self-explanatory program";
    char *teststr2 = "123 is the number";
    int count;
    
    /* count number of initial lowercase letters */
    count = StrSpn(teststr, "abcdefghijklmnopqrstuvwxyz");
    printf("%d\n", count);
    And the answer should be 4.



    Using only C and stdio.h

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    the function as described takes two char * arguments (well, really should be const char *, but anyway), but you have it protoypted taking a char * and an int
    hello, internet!

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's not that hard. Pseudo code:
    Code:
    SET Counter TO 0
    
    SET i TO 0
    
    LOOP WHILE i < LENGTH OF String
    
       IF String[i] EQUALS TO Character THEN
    
          INCREASE Counter BY 1
    
       END IF
    
       INCREASE i BY 1
    
    END LOOP  
    
    RETURN Counter
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    What about for this one?
    Code:
    int StrCSpn(char *str, char *set);
    StrCSpn works like StrSpn except that the sense of how the character is in the second argument is reversed. That is, starting from the beginning of the first character string argument, StrCSpn counts the number of consecutive characters in the string that consist of characters NOT included in the second argument string.

    Would that be this?
    Code:
    char *str;
    char *set;
    int i = 0;
    int counter;
    
    int StrSpn(char *str, int ch)
    {
    	long nc;
    
    	nc = 0;
    	while(getchar() != 0)
    		++nc;
    
    	while(i < nc)
    	{
    		if(str != set)
    			counter++;
    		
    		i++;
    	}
    
    	return counter;
    }
    If so i get a few errors that I'd need help resolving.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your function prototype looks like this:
    >>int StrCSpn(char *str, char *set);
    and yet you function header looks like this:
    >>int StrSpn(char *str, int ch)
    Why have you changed the type of parameters being passed?

    >>while(getchar() != 0)
    getchar() gets a character from stdin (the keyboard normally). This isn't in the programs requirements spec.

    Avoid the use of global variables in this program, too.

    Try thinking about it before writing the code, and write your own psuedo code. Then convert that to C.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Ya i had to fix that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM