Thread: expanding function

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    expanding function

    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.

    How exactly am i supposed to handle a-b-c? Just print out abc?

    And what about z-a. Should i print thise backwards, from z to a?

    How about handling -a-z, how to handle this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    How exactly am i supposed to handle a-b-c? Just print out abc?
    Ask your teacher what "a-b-c" means. Certainly, printing out "abc" is one option, but another option is to regard it as an error.

    Quote Originally Posted by Tool
    And what about z-a. Should i print thise backwards, from z to a?
    Again, ask your teacher what "z-a" means. It makes sense to print the letters from z to a in that order, but it could also make sense to print them from a to z, or to regard it as an error.

    Quote Originally Posted by Tool
    How about handling -a-z, how to handle this?
    You could just check to see if the first (and/or last) character is a dash.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Well i wish i had a teacher, im just self studying from the C book.

    I did write the code for this, based on another code i found and its output looked like this - see attachment.

    Anyway, this is the code i wrote...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_SIZE 1024
    
    void expand (char dest[], char s[])
    {
         int x = 0, y, z = 0;
         
         while(s[x] !='\0')
         {
            if((x == 0 || s[x+1] == '\0') && s[x] == '-') /* first or last char is a dash */
               dest[z++] = s[x];
              
            else if(s[x] == '-')
            {
                 if(s[x-1] >='0' && s[x-1] <='9')  /* checking left char */
                 {
                     if(s[x+1] >='0' && s[x+1] <='9') /* checking right char */
                     {
                          if(s[x-1] < s[x+1])
                          {
                             for(y = s[x-1]; y <= s[x+1]; y++)
                             dest[z++] = y;
                          }
                          else if(s[x-1] == s[x+1])
                          dest[z++] = s[x-1];
                          else
                          {
                              for(y = s[x-1]; y>= s[x+1]; y--)
                              dest[z++] = y;
                          }
                          
                     }
                     else if((s[x+1]>='a' && s[x+1] <='z') || (s[x+1]>='A' && s[x+1] <='Z'))
                     printf("Mismatching argument to expand.\n");
                     else dest[z++] = s[x];
                 }
                 else if(s[x-1] >='a' && s[x-1] <='z')
                 {
                      if(s[x+1] >='a' && s[x+1] <='z')
                      {
                           if(s[x-1] < s[x+1])
                           {
                                for(y = s[x-1]; y<=s[x+1]; y++)
                                dest[z++] = y;
                           }
                           else if(s[x-1] == s[x+1])
                           dest[z++] = s[x-1];
                           else
                           {
                               for(y = s[x-1]; y>=s[x+1]; y--)
                               dest[z++] = y;
                           }
                      }
                      else if((s[x+1] >='0' && s[x+1]<='9') || (s[x+1]>='A' && s[x+1] <='Z'))
                      printf("Mismatching argument to expand.\n");
                      else dest[z++] = s[x];
                 }
                 else if(s[x-1] >='A' && s[x-1] <='Z')
                 {
                      if(s[x+1] >='A' && s[x+1] <='Z')
                      {
                           if(s[x-1] < s[x+1])
                           {
                                  for(y = s[x-1]; y<=s[x+1]; y++)
                                  dest[z++] = y;
                           }
                           else if(s[x-1] == s[x+1])
                           dest[z++] = s[x-1];
                           else
                           {
                               for(y = s[x-1]; y>=s[x+1]; y--)
                               dest[z++] = y;
                           }
                      }
                      else if((s[x+1] >='0' && s[x+1] <='9') || (s[x+1] >='a' && s[x+1] <='z'))
                      printf("Mismatching argument to expand.\n");
                      else dest[z++] = s[x];
                 }
                 else dest[z++] = s[x]; /* a leading -, just put it in the string */
            }
            else if(s[x+1] == '-') /* check if leading dash */
            {
                 if((s[x] >='0' && s[x] <='9') || (s[x] >='a' && s[x] <='z') || (s[x] >='A' && s[x] <='Z'))
                 ;
                 else dest[z++] = s[x];
            }
            else if(s[x-1] != '-' && s[x+1] !='-') 
               dest[z++] = s[x];
          
            x++;  
         }
         dest[z] = '\0';
    }
    
         
         
         
    int main(int argc, char *argv[])
    {
         
         char s[MAX_SIZE] = "-r-a--";
         char destination[MAX_SIZE];
         
         expand(destination, s);
         printf("%s", destination);
         
         putchar('\n');
         system("PAUSE");    
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM