Thread: Expand nightmare!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Unhappy Expand nightmare!

    So here today I am writing this code that will expand on the test string I have created ie. a-f which will write out abcdef. I have most of my logic hopefully correct but the debugger hates my expand call function.
    I was wondering what I am doing wrong, because it really hates me and I have been trying to fix it and can't seem to understand what is going on.
    I have read on the arrays section and I thought i knew what I was doing......

    At last here is my code any help on my expand function would be very appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char s[100];
    int j;
    void expand(char from, char to)
    
    main()
    {
        char s[]="this is a-f test";
        int len = 16;
        int i;
        
        for (i=j=0; i < len; i++)
        {
            if (isalpha(s[i]) || isdigit(s[i]))
            {
                if (s[i+1] =='-')
                {    if (isalpha(s[i]) && isalpha(s[i+2] && s[i] < s[i+2])
                    
                        expand(s[i],s[i+2]);
                    
                    else if (isdigit(s[i]) && isdigit(s[i+2] && s[i] < s[i+2])
                    
                        expand(s[i],s[i+2]);
                    
                    else 
                        printf("invalid statement\n");
                }
            }
        }
        else
            s1[j++]=s[i];
    }
    
    
    
    void expand(char from, char to)
    {
        char c = from;
        
        while (c <= to)
            s1[j++]=c++;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char s[100];
    int j;
    void expand(char from, char to)
    1. Why are those global?
    2. If that is supposed to be a function prototype, then you should have a ; on the end of it.


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

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Additionally, while we are correcting before fixing, you should learn How to define main() and How to ask a smart question.

    As for a prelim response prior to, I would say that your compiler "hates" you because you aren't writing standard compliant code.

    Now that my one-liner is done, you are going about the logic of your simple parser all wrong and making this needlessly complex. What is your end product here? A basic translator?
    Last edited by AndrewHunter; 10-13-2011 at 12:22 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    So after a week of redoing this program im so close but I still am having trouble with my expand function I can get it to process the char, but still wont expand it maybe I have it wrong idk can someone take a look and guide me in a direction?

    basically your type in like a-f or 1-4 and it will expand to output abcdef or 1234

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void expand(char s1[], char s2[]);
    int getline(char s1[], int lim);
    
    int main(void)
    {
        char s1[100];
        char s2[100];
        getline(s1, sizeof(s1));
        expand(s1,s2);
        printf("%s\n %s\n", s1, s2);
        
         return 0;
    }
    
    
    
    void expand(char s1[], char s2[])
    {
        
        int j, i;
        int len = strlen(s1);
        
        for (i=j=0; i < len; i++)
        {
            int processed = 0;
            char from = s1[i];
            char to = s2[i+2];
        
            if (s1[i+1] =='-')
                if ((isalpha(from) && isalpha(to))||(isdigit(from) && isdigit(to)))
                {
                    while (from <= to)
                    {
                        s2[j++]=from++;
                        processed = 1;
                    }
                }
                if (!processed)
                    s2[j++] = s1[i];
                s2[j] = '\0';
        }
    }
    
    int getline(char s[], int lim)
    {
        int i, c;
    
        for(i=0; i < lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
            s[i] = c;
        if (c == '\n')
        {
            s[i] = c;
            ++i;
        }
        s[i] = '\0';
        return i;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On this line of code, when i is on it's last loop, what will i+1 be referring to?
    Code:
    if (s1[i+1] =='-')
    You're out of bounds. Check for the hyphen behind i, instead of ahead of it, perhaps.
    Or, use a flag, so you never check past the array's bound. When you reach the hyphen, you set the flag, and never check for the hyphen again.

    Show me your expected input, and your output example from it. It's hard to believe you need half this amount of code to print up a-f or 1-4, even considering your limit variable.

    A=abcdef limit 6
    1=1234 limit 4
    B=?
    20=?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void expand(char s1[], char s2[])
    {
        int j, i;
        int len = strlen(s1);
        
        for (i=j=0; i < len; i++)
        {
            int processed = 0;
            char from = s1[i];
            char to = s2[i+2];
        
            if (s1[i+1] =='-')
                if ((isalpha(from) && isalpha(to))||(isdigit(from) && isdigit(to)))
                {
                    while (from <= to)
                    {
                        s2[j++]=from++;
                        processed = 1;
                    }
                }
                if (!processed)
                    s2[j++] = s1[i];
                s2[j] = '\0';
        }
    }
    You need to sit down and decide *exactly* what you want this function to do...
    You are feeding it two strings... from and to ... where is your output?
    Your function will fail if s2 is a string literal.

    Also you are not inserting characters... if you fed it "hello 1-9 world" ... you would get back "hello 123456789" not "hello 123456789 world".


    What you need is a function to parse the input string, another to get the expansion string and yet another function to insert that string into your existing string...
    Code:
    int Expand(char *dst, const char from, const char to)
      { char x, i = 0;
         for( x = from; x <=to; x++)
           { dst[i] = x;
              i++ }
         return i;  }
    
    int Insert(char *dst, char *src, int pos, const char *ins)
      {  int i;j;
          int x = strlen(ins) + pos;
          for (i = 0; i < pos; i++)
            *dst = *src;
          for (j = pos; j < x; j++
            *dst = *ins;
          while (*src != 0)
              *dst[j++] = *src[pos++];
          return 1; }
    
    int Parse(char *str)
      { int i, len = strlen(str);
         char ins[64];
    
        for (i = 0; i < len; i++)
           { if (str[i] == '-')
               {  Expand(ins,str[i-1],str[i+1]);
                  Insert(str,str,i-1,ins); } }
        return strlen(str); }
    Now this is just "top of the head" it's not going to work first try... but that's about the way I'd go at it...

    Hint: Think in smaller blobs...
    Last edited by CommonTater; 10-18-2011 at 01:43 AM.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    well im trying to do for my input a-f, I thought i got through it by looping the string with either a 1 or 0 as processed or not, it will run through the entire string until it has processed all the characters. so I do not understand what you are saying tater when you say that it will print up to the expand then end the string... maybe I am not that familiar with what you are trying to say i guess?

    With my i+1 I am checking each char to see if it has a '-' after each char, if it does not have a '-' it gets marked with a 0 and gets thrown in the j string and keeps going, I thought that after the expand it still would keep going until it runs out of characters.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It will... but you will end up with 0s in your j string... cstrings are 0 terminated... The string will end with the first 0 you put in there.

    Also, at no time do you actually insert the expanded list of characters...

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Do you think I should put a different number for the character if it is not have a - in front of it? Also can you explain more on the insert the expanded list of characters?? I'm confused by what you are saying?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... the job according to your original posting's description is to expand groups of characters separated by a dash...

    So, if the input string is "This is an expander test 1-9 that should work"

    The output of your function(s) should be "This is an expander test 123456789 that should work"

    If you do not *insert* the expanded list you would get... "This is an expander test 123456789should work" because you will be overwriting the tail end of your string.

    If you simply insert a 0 as a marker you will accidentially null terminate the string leaving you with "This is an expander test "

    Even in the case when the only thing in the string is "1-9" you risk getting into trouble with allocated space... You will be making the string longer.

    See? This is where you need to sit down with pencil and paper and work this stuff out in advance...

    What *exactly* are you feeding that function?
    What *exactly* do you want back?
    Where are you putting the result?
    etc.

    Think in smaller blobs...
    Last edited by CommonTater; 10-18-2011 at 11:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expand
    By ASCII in forum C Programming
    Replies: 11
    Last Post: 10-03-2011, 04:01 AM
  2. nightmare!! I really need help with this!
    By codebot in forum C Programming
    Replies: 7
    Last Post: 09-19-2010, 05:46 AM
  3. Expand an array
    By SlyVixsky in forum C Programming
    Replies: 9
    Last Post: 08-01-2009, 11:28 PM
  4. DVD nightmare
    By geek@02 in forum Tech Board
    Replies: 9
    Last Post: 01-21-2007, 04:49 PM
  5. expand(int s[]) 3-3 knr
    By olbas in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 11:01 AM