Help - rewriting style of string library in C

This is a discussion on Help - rewriting style of string library in C within the C Programming forums, part of the General Programming Boards category; Hello im an 2nd year university student, and i am stuck on an assignment. I am supposed to store the ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    16

    Help - rewriting style of string library in C

    Hello im an 2nd year university student, and i am stuck on an assignment.

    I am supposed to store the length of the string in the zero'th element of the array, and the remaining characters after. No null terminator is used in this setup. My assignment is to implement this style of string library in C.

    Im not expecting for someone to do the assignment, but i have been having a very hard time understanding the how to use strings and array pointers, so you can probably tell i would have difficulty to this.

    I have these guide lines:

    1) The functions ps_cat, ps_cmp, and ps_cpy should mimic the corresponding routines strcat, strcmp and strcpy in the C standard library.

    2) ps_set copies a null-terminated C string into a pstring

    3) ps_put should output the pstring on stdout.

    4) ps_set and ps_cat should make sure that no overflow occurs, and either abort or truncate the string. Make sure you document your choice for each function.


    You should use the following declarations:
    Code:
    #define PSTRING_MAX 126
    typedef char pstring[PSTRING_MAX+1];
    void ps_cat(pstring dest,const pstring src);
    int ps_cmp(const pstring a, const pstring b);
    void ps_cpy(pstring dest,const pstring src);
    void ps_put(const pstring);
    void ps_set(pstring dest,const char *);
    and i have an example program:
    Code:
    int main(void){
    
      pstring a, b;
    
      ps_set(a,"hello ");
      ps_cpy(b,a);
      ps_set(a,"world\n");
      printf("%d %d %d\n", ps_cmp(a,b),
             ps_cmp(a,a),
             ps_cmp(b,a));
      ps_cat(b,a);
      ps_put(b);
    }
    which gives the following output:
    Code:
    1 0 -1
    hello world
    I am very lost, and don't know how to tackle such a program, i understand what the example program is doing, but don't know how to write my own ps_cat, ps_cmp, and ps_cpy functions.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You will probably be overwhelmed and get confused if you look at the "big picture". So dont look at all of the functions, prototypes, example, requirements, etc. Start with just one function and focus entirely on that.

    The core one that ties them together, I think, is "ps_set", which you can think of as a "constructor" (if you dont know what that is dont worry). So for "ps_set(a, "hello")" the function is to copy the array of chars--one by one--to "a" starting at index 1, a[1]. You continue copying characters until you reach the terminating null "\0" character. The length of the string is the number of characters you copied, so you store this length at index 0 of a, a[0].

    Consider yourself lucky to have an interesting and useful (for learning purposes, not practical purposes) assignment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Yes i am getting over whelmed by the entire program.

    I have been looking everywhere about this, and people are saying that it would be pointless to rewrite the functions because they are about as good as they get, the only other reason would be for practice.

    I have a question about the ps_set parameters..
    void ps_set(pstring dest,const char *);
    what is (const char *) pointing to??

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Yes, thats what I was saying. Its not practical to write this as it already exists. It is very useful for learning purposes, as I said above.

    For the example " ps_set(a,"hello") ", the second argument "hello" is a "const char*). Its constant because its a string literal (opposed to some variable which can change, lets say). Its a "char*" because its a 'string' as you are familiar with Im sure.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    ok so i just worked this out on paper.
    example: " ps_set(a,"hello") "
    would put the word "hello" in an array starting at a[1]



    Code:
    void ps_set (pstring dest, const char string){
      int i = 1, b = 0;
    
      while (getchar() != '\n') {
         if (b <= PSTRING_MAX) {
           dest[i] = getchar();
           b++;
           i++;
        }
      }
    }
    but how do i find the length of the array?
    dest[0] = sizeof(dest)
    would find the length of everything wouldn't it? including the dest[0]...?
    Last edited by ~C_Student~; 11-18-2009 at 01:51 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by ~C_Student~ View Post
    ok so i just worked this out on paper.
    example: " ps_set(a,"hello") "
    would put the word "hello" in an array starting at a[1]



    Code:
    void ps_set (pstring dest, const char string){
      int i = 1, b = 0;
    
      while (getchar() != '\n') {
         if (b <= PSTRING_MAX) {
           dest[i] = getchar();
           b++;
           i++;
        }
      }
    }
    but how do i find the length of the array?
    dest[0] = sizeof(dest)
    would find the length of everything wouldn't it? including the dest[0]...?
    #1. The second argument should be a const char *. You forgot the * part.

    #2. Why are you using getchar? You should be copying data from the source string not attempting to get input from the user.

    #3. How do you find the length of the array? How about strlen? Use that on the source string (not your destination pstring) and if it's less than or equal to PSTRING_MAX then you can continue with the function and store the value in array location 0. If it's more than PSTRING_MAX, then you should exit the function without doing anything.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to find the length of the array:
    Code:
    for( s = 0, d = 1; input[ s ]; s++, d++ )
        output[ d ] = input[ s ];
    output[ 0 ] = s;
    The real problem however is that you call getchar twice, once in the loop to make the assignment, and once at the stop-check of each loop. So unless you enter an even number of characters, counting the \n, it's not going to stop when you hit enter.


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

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Ok so i think i got the ps_set function done, hows it look

    Code:
    void ps_set(pstring dest, const char *test){
      int i, b,  len;
      for (i = 1; i != '\n' && b <= PSTRING_MAX; i++) {
        dest[i] = test[i - 1];
        b++;
      }
      len = strlen(test);
      dest[0] = len;
    }
    One other question, how is strcat composed? like how does it work, i think i have ps_set, ps_cpy, ps_put, ps_cmp done well enough, but i don't really know how strcat works to begin working on my own ps_cat version.
    Last edited by ~C_Student~; 11-19-2009 at 08:03 AM. Reason: fixed errors

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    #1. You haven't initialized b to anything (0 perhaps).

    #2. len is unneeded, simply set dest[0] to the value returned directly from the strlen function. You could also just set this location to b and skip the strlen function since by the time you reach the end of the loop, b will contain the length of the string.

    #3.
    Code:
    for (i = 1; i != '\n' && b <= PSTRING_MAX; i++) {
    What are comparing against here? i is an integer value that simply gets incremented by one each time through the loop. I think the integer value of '\n' is 10 so this just means that your function will stop copying characters when you've reached the 10th one but dest[0] (the length of your pstring) would then indicate a possibly much greater value for the length. I think you mean:
    Code:
    for (i = 1; test[i] && b <= PSTRING_MAX; i++) {
    That part will be true as long as we haven't reached the end (null terminator) of the source string.




    Quote Originally Posted by ~C_Student~
    One other question, how is strcat composed? like how does it work,
    It walks the destination string until it finds the null character, then it starts copying from the source string to the destination string at that point until it reaches the null character in the source string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Originally Posted by hk_mp5kpdw
    It walks the destination string until it finds the null character, then it starts copying from the source string to the destination string at that point until it reaches the null character in the source string.
    So if im putting two strings together lets say:

    a = "world"
    b = "hello "

    in my program it is called like this

    Code:
      ps_cat(b,a);
    but wouldn't b already have the strlen = 6 ? How could i add another 5 char array slots to it? Do i change the length of b? b = b_len + a_len, then start putting a at b_len +1

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    If ps_cat is supposed to work like strcat, then b cannot be "hello ". Rather, it must be an array of char with at least 12 characters, the first of which has the value of 6, and the next 6 making up "hello ". This way, you are able to concatenate "world" to it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by ~C_Student~ View Post
    So if im putting two strings together lets say:

    a = "world"
    b = "hello "

    in my program it is called like this

    Code:
      ps_cat(b,a);
    but wouldn't b already have the strlen = 6 ? How could i add another 5 char array slots to it? Do i change the length of b? b = b_len + a_len, then start putting a at b_len +1
    Yes, b's initial length would be 6 but remember that it's simply a value representing the current last valid/used index of the array. You've got PSTRING_MAX total slots to work with here in your pstrings.

    You aren't really adding array slots, they already exist in the pstring (there are PSTRING_MAX of them available) but the length value indicates which ones are "in use"... any slots after that (up to PSTRING_MAX) should be considered empty and available for use.

    Yes you would alter the stated length of b at the end of the function to be b's original length plus a's length.

    Yes, you would start putting the characters from a at b's current length plus 1 and keep going until you either reach the end of a or when you reach PSTRING_MAX total characters in b.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Quote Originally Posted by hk_mp5kpdw View Post
    *insert stuff you said*
    Thanks for explaining that it totally makes sense now, im gonna go see if i can code it now

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    what does this error mean, i been messing with it but can't make it go away

    Code:
    new.c: In function ‘ps_put’:
    new.c:87: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pretty much just what it says. It wants you to pass a character pointer, and instead, you're passing an integer.
    Code:
    int x = 0;
    printf( "%s", x );

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

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. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Replies: 12
    Last Post: 06-08-2005, 11:23 AM

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