Expand

This is a discussion on Expand within the C Programming forums, part of the General Programming Boards category; Hi! I've been working through the C Programming Language Second Edition, and I'm stumped on one of the exercises. Exercise ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    Expand

    Hi! I've been working through the C Programming Language Second Edition, and I'm stumped on one of the exercises. Exercise 3-3 has me baffled. The task is as follows:
    Exercise 3-3. Write a function expand(s1,s2) that expands shorthand notations like a-z in the
    string s1 into the equivalent complete list abc...xyz in s2. Allow for letters of either case and digits,
    and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading or trailing -
    is taken literally.

    So far I've written the expand function as:
    Code:
    void Expand(char s1[], char s2[]) {
        int i = 0;
        int y = 0;
        while((s2[i++] = 0) != '\0') /* clear  s2 */
    	;
        for(i = 0; s1[i] != '\0'; ) {
    	int start = s1[i++];
    	/* advance i until a new type is found to handle cases like a-b-c-d */
    	if(isdigit(start)) {
    	    for( ; isdigit(s1[i]) || s1[i] == '-'; i++)
    		;
    	}
    	else if(islower(start)) {
    	    for( ; islower(s1[i]) || s1[i] == '-'; i++)
    		;
    	}
    	else if(isupper(start)) {
    	    for( ; isupper(s1[i]) || s1[i] == '-'; i++)
    		;
    	}
    	else {		   /* if they are not the same type, exit the function */ 
    	    printf("Invalid character\n");
    	    return;
    	} 							       
    	int end = s1[i-1];
            /* check if start and end are the same type, and don't go over bounds */
    	if(isdigit(start) && isdigit(end)) {
    	    while(start <= end)
    		s2[y++] = start++;
    	}
    	else if(islower(start) && islower(end)) {
    	    while(start <= end) 
    		s2[y++] = start++;
    	}
    	else if(isupper(start) && isupper(end)) {
    	    while(start <= end) 
    		s2[y++] = start++;
    	}
    	else {
    	    printf("Invalid character");
    	    return;
    	}
        }
        return;
    }
    However, for some reason if I pass it a string like a-z0-9, it is adding on -9 at the end of it. My output would be: abcdefghijklmnopqrstuvwxyz0123456789-9

    Can someone please help me diagnose the problem, I'm completely baffled.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to nul terminate your s2 string when you are done sticking things in it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Thank you, that fixed it. I don't understand where the extra characters were coming from however, could you please explain?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If you don't terminate your string with a '\0', printf will continue past the end of the string until it finds one somewhere else. In your case, I think it is picking up the remnants of the input string, ie. the last few characters of "a-z0-9". But this behaviour is not guaranteed; it depends on what is stored where in memory. When I tried it I got several lines of gibberish.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by ASCII View Post
    Thank you, that fixed it. I don't understand where the extra characters were coming from however, could you please explain?
    Alongside H's serious explanation, here is a silly joke that illustrates the point .
    Two strings walk into a bar and sit down.
    The bartender says, “So what’ll it be?”
    The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”
    “Please excuse my friend,” the second string says, “He isn’t null-terminated.”
    Last edited by manasij7479; 10-02-2011 at 03:29 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That joke doesn't really work in C, because you aren't a string if you aren't nul terminated. I know it's supposed to be a C joke, but it just bug people like me.

    /buzzkill


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by quzah View Post
    That joke doesn't really work in C, because you aren't a string if you aren't nul terminated. I know it's supposed to be a C joke, but it just bug people like me.

    /buzzkill

    Quzah.
    Ok.. modification.. [Two const char* `s walk into,....~] but the string version is much more repeatable in human company...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A string and a dude that you think is a string walk into a bar...


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    A string and a dude that you think is a string walk into a bar...
    Quzah.
    A guy with a sense of humour and Quzah walk into a bar...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    A guy with a sense of humour and Quzah walk into a bar...
    A guy with a lousy sense of humor and Quzah walk into the bar...


    I've got a great sense of humor. That joke just wasn't funny.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I've got a great sense of humor.
    Notwithstanding evidence to the contrary...


    That joke just wasn't funny.
    Ok... no argument on that part.

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by quzah View Post
    That joke just wasn't funny.
    Of course it wasn't.
    Only totally pointless jokes can be funny.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expand an array
    By SlyVixsky in forum C Programming
    Replies: 9
    Last Post: 08-02-2009, 12:28 AM
  2. C# compiler expand our code?
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-03-2008, 02:41 AM
  3. How to expand out of command
    By alexnb185 in forum C Programming
    Replies: 4
    Last Post: 07-08-2007, 05:35 PM
  4. expand(int s[]) 3-3 knr
    By olbas in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 12:01 PM
  5. CProgramming.com should expand!
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 10-05-2001, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21