Like Tree1Likes
  • 1 Post By root4

Expansion

This is a discussion on Expansion within the C Programming forums, part of the General Programming Boards category; Hi, I'm working through the problems in K&R and I'm having difficulty with a particular one. Code: /*Write a function ...

  1. #1
    rbz
    rbz is offline
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    Expansion

    Hi, I'm working through the problems in K&R and I'm having difficulty with a particular one.

    Code:
    /*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.*/
    
    
    
    void expand (char s1[], char s2[]){
      
      int i = 0,j = 0;
      
      
      
      while(s1[i] != '\0'){
        if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-'){   //1st or last char is a "-"
          s2[j++] = s1[i];
           else if(s1[x] == '-'){
              if(s1[i-1] >= '0' && s1[i-1] <= '9'){ // check if the left char is a 0 or a 9 eg. 0-9, 9-0
                     if(s1[i+1] >= '0' && s1[i+1] <= '9'){ //check the same for the right char
                        if(s1[i-1] <= s1[i+1]){
                  for(q = s1[i-1]; q <= s1[i+1]; q++)
                          s2[j++] = q;}
    So far I think that this code checks if the leading characted is a "-" and copies it and checks if the string starts with a 0 or a 9 on and ends with a 0 or a 9. However, I'm not confident at all at what I've done and any criticisms and pointers would be much welcomed. I realize I could use pointers, but i'm avoiding it just because it hasn't come up in the book quite yet. Even though their solution does use pointers.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I suggest you indent you code properly first.

    If you're not confident about your code, write *tests*.
    As you have no side effect in your example, it's pretty easy.
    I assume s2 has to be allocated beforehand (this is not really convenient, how much memory should be allocated for it?).
    Code:
    #include <assert.h>
    ...
    int main(void) {
    	char *in = "0-9";
    	char out[512]; /* allocate a big chunk of memory, how much is needed? */
    	char *expected = "0123456789";
    	expand(in, out);
    	assert(!strcmp(out, expected));
    }
    This is just a draft example, organize the tests into functions according to your needs.
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Macro Expansion
    By anirban in forum C Programming
    Replies: 3
    Last Post: 09-03-2012, 12:20 AM
  2. Expansion Crystal and AGP
    By jrahhali in forum Tech Board
    Replies: 0
    Last Post: 08-17-2011, 09:04 AM
  3. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-29-2008, 12:26 AM
  4. Macro expansion
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 11-02-2007, 04:08 AM
  5. Expansion
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-02-2001, 06:45 AM

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