string parsing

This is a discussion on string parsing within the C Programming forums, part of the General Programming Boards category; Hi, I'm doing a small string parser, the main problem is that I didn't want to create 10 different char ...

  1. #1
    Banned
    Join Date
    Aug 2005
    Posts
    9

    string parsing

    Hi,

    I'm doing a small string parser, the main problem is that I didn't want to create 10 different char arrays, so I did with only one. It's causing me a buffer overflow...

    Code:
    #include <stdio.h>
    
    int main()
    {
        char buff[BUFSIZ], *words[];
    
        printf("Enter the numbers (separated by space):\n");
        fgets(buff, sizeof(buff), stdin);
        sscanf(buff, "%s %s %s %s %s %s %s %s %s %s", words[0], words[1], words[2], 
               words[3], words[4], words[5], words[6], words[7], words[8], words[9]);
    
        int i;
        for(i=0; i > 10; i++)
           printf("The %d word is %s\n", i, words[i]);
    
        getchar();
        return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    char *words[]; does not allocate any space for anything. I'm surprised it even compiles.

    Try:
    Code:
    char words[10][50];
    instead. Just make sure none of the individual strings are more than 49 characters.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2005
    Posts
    9
    It still dosen't work... Altough there's no overflow, when you hit enter nothing happens...

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    for(i=0; i > 10; i++)
    Are you sure that is what you mean?

  5. #5
    Banned
    Join Date
    Aug 2005
    Posts
    9
    Oh I see... Thanks

  6. #6
    Banned
    Join Date
    Aug 2005
    Posts
    9
    Another question:

    Wich function would I use to see if two strings share the same charset?

    like: agdf and fadg share the same charset

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I don't think there's a built-in C function to do this, but you could code one yourself, in fact, I had to do this just a small while ago (like, yesterday?). I used a thing where I squared the ASCII value of each character and then summed it into a small "checksum", and then checked results to see if they were the same. Squaring or cubing or whatever reduces the risk of like, having some of 'abc' matching 'bbb', just because they have to same un-squared ASCII sums. For a more reliable method, you could store each in an int array of [26] and then every time an 'a' occurs you would increment iArray[0] and with 'b' iArray[1]. Naturally this method is considerably more "costly", but it'll definitely be much more reliable.

    Edit: here's the code I used http://pastebin.com/341079 uses vectors and fstreams and ostreams and other nasty things that really aren't C, but just some concept eh?

  8. #8
    Banned
    Join Date
    Aug 2005
    Posts
    9
    I must say that I never would imagine that Thanks a lot!

    Would my function be correct then:

    Code:
    int asciisum (char text[21])
    {
        int checksum = 0, i, textlen = strlen(text);
        for(i=0; i <= textlen; i++)
            checksum += ((int)text[i]) * ((int)text[i]);
        return checksum;
    }
    Last edited by WizardOfOz; 08-19-2005 at 02:59 PM.

  9. #9
    Banned
    Join Date
    Aug 2005
    Posts
    9
    Tonto how did you manage to copy and paste it all in under 30 seconds? I just finished my program (I swear I didn't copy yours but the asciisum function, I barelly couldn't read it with all this OOP stuff), and it finishes in under 0.01 seconds but I can't manage to copy and paste in time...

    http://pastebin.com/342029

    I really hope you didn't use sockets...

    Offtopic:

    Hehe a one-way hash: (I'm bored...)

    Code:
    int asciisum (char* text)
    {
        int checksum = 0, i, textlen = strlen(text);
        for(i=0; i <= textlen; i++)
            checksum += ((int)text[i]) * ((int)text[i]);
        return checksum/textlen;
    }
    Last edited by WizardOfOz; 08-20-2005 at 10:51 PM.

  10. #10
    Banned
    Join Date
    Aug 2005
    Posts
    9
    New link... http://pastebin.com/346434

    Bump?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about asking a question if you have one instead of just being a jackass?


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

  12. #12
    Banned
    Join Date
    Aug 2005
    Posts
    9
    If you actually managed to move your little head 3 inches you would see my question.

    You really think you rock don't you?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by WizardOfOz
    If you actually managed to move your little head 3 inches you would see my question.
    Oh, right. In the post you made, this was your question:
    Quote Originally Posted by WizardOfOz
    New link... http://pastebin.com/346434

    Bump?
    Silly me.


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

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Which question are you referring to?

    this one?

    >> Wich function would I use to see if two strings share the same charset?

    or this one?

    >> Would my function be correct then:[?]

    or this one?

    >> Tonto how did you manage to copy and paste it all in under 30 seconds?

    None of the above seem to help me at all - but hey, maybe its just me. Oh, that's right, quzah did not know what you wanted either. You might try asking a specific question, and then you just might get a specific answer.

    edit:: oh I missed that fourth one - good eyes quzah

  15. #15
    Banned
    Join Date
    Aug 2005
    Posts
    9
    I hate the funny ones.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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