Thread: I need help with implementing strings???

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    I need help with implementing strings???

    Hi? I was wandering if any one knows anything about implementing and using strings..
    Basically i have a basic understanding of C programming and would like someone to start the problem for me...

    Basically i have to implement a function called getString() which extracts a string from standard input; stores it in an array; and returns a pointer to its starting character.

    Each call to getString() locates the next string in input and stores it after any previously stored strings.

    In storage each string is terminatted by the ASCII null character; the delimmiting quotes are omitted.

    some extra notes:

    Storage overflow must be prevented; ie if the next string to be deposited in the array would exceed its bound then extra memory must be allocated.

    The same string must not be stored more than once, ie, if a string is encountered twice, then getString() is to return a pointer to where it was stored on first encounter.

    Then i have to implement a function called dumpString(), ie, each call to dumpString() returns a pointer to the next string in storage (starting with the first); returning a NULL pointer at the end of the list.

    Also "a quote character \"inside\" a string must be escaped with the backslash character, as is the backslash character itself."

    If anyone can help me with this problem that would be great, otherwise give me some links that i could refer to for this sought of problem..


    i have started something, but not sure if is correct...

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define BUFFER_LEN 256

    void main()
    {
    //declare and initialise variables
    char Buffer[BUFFER_LEN];//stores input
    char str[40]; //char string

    //declare function getString
    void getString(char *);

    //declare function dumpString
    void dumpString(char *);

    printf("Input a string please!!");
    fgets(str);
    //read user input
    getString(str); //read input
    Last edited by atif; 04-05-2002 at 04:03 AM.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    You ar making something like 'strtok()' i suppose

  3. #3
    Registered User M!ke's Avatar
    Join Date
    Apr 2002
    Posts
    3

    Question Re: I need help with implementing strings???

    What happend to your ORIGINAL posting ??

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    Wink Hi? can you help me

    Hi Mike how are ya?what do you mean what happened to my original posting??

    would you know how to attempt this sought of problem...
    or can you give me some links, where i can get some ideas of how to solve this problem .. please.

    i have attempted something could you please check and correct me anywhere.. i need to implement 2 functions getString() and dumpString()

    I'm not asking you to write the whole program for me, just help me along the way thanks..

    kind regards Atif.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    void main is wrong and undefined, main returns an int.

    >fgets(str);
    fgets takes three parameters, the array to assign the string to, the size of the array, and the stream to read from.

    >Storage overflow must be prevented; ie if the next string to be
    >deposited in the array would exceed its bound then extra memory must be allocated.
    Don't use an array if you need to resize it, use a char * and realloc to increase and decrease the size of the buffer as needed.

    >Each call to getString() locates the next string in input and
    >stores it after any previously stored strings.
    This screams linked list to me.

    >returning a NULL pointer at the end of the list
    This REALLY screams linked list

    >give me some links that i could refer to for this sought of problem..
    Say please...
    http://www.cprogramming.com/tutorial.html

    -Nitpicker v1.0 - 0% useful. 50% annoying. 100% correct.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    implementing strings using linked lists??

    HEy prelude,
    thanks for your reply..so you think the best way to solve this problem is to use linked lists...

    could you please... show me how to use linked lists or give me some examples of how i can implement a linked lists with strings..

    thanks ...waitin for your reply..

    Atif

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    he did give you an example.
    follow the link.
    you are looking for : lesson15.html The basics of singly linked lists
    good luck

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    Linked Lists in C prog

    Sorry, but i was asking about Linked Lists in C programming not in C++ . So , is there any links to linked lists in C prog.
    specifically to do with strings..

    Thanks
    Atif

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So , is there any links to linked lists in C prog.
    Linked lists in C++ are really the same as in C, just swap out
    node->next = new NODE;
    with
    node->next = malloc ( sizeof *node );

    The link I gave you is implemented with C++, but could easily be converted to C with a minimum of effort. Give it a shot and you'll learn more than if we just gave you the code.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM