string handling

This is a discussion on string handling within the C Programming forums, part of the General Programming Boards category; I need to implement my own set of functions which perform standard string handling functions (such as strcpy, strcmp, etc) ...

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

    Red face string handling

    I need to implement my own set of functions
    which perform standard string handling functions
    (such as strcpy, strcmp, etc) using array, ptr, malloc...

    Started writing the first part with someone's help but I'm having difficulty...
    Just began to learn C language so need your help please.
    Please give me some ideas as to how I should arrays and ptr
    to write these functions. thank u.

    /* String package */

    #include <stdlib.h>
    #include <stdio.h>
    #include "mystring.h"

    const int EXTRA_SPACE = 10;


    /* (1) perform strcpy function */
    String NewString(char *s)
    {
    int originalString;
    originalString = length(s);

    char *myPtr;
    myPtr= malloc (originalString + 1 + EXTRA_SPACE);


    for(int i=0; i <= originalString; i++){
    myPtr[i] = s[i]; }

    String myString = malloc(size_of(struct String));
    myString->ptr = myPtr;
    myString->arraySize = originalString + 1 + EXTRA_SPACE;

    return myString;
    }

    int length(char *ptr){
    }

    /* (2) deletes all dynamic storage associated with "string" */
    void DeleteString(String string){}

    /* (3) return a character at index */
    char char_at(String string, int index){
    }

    /* (4) eg. "programming" substring(string,3,7) return "gram" */
    String substring(String string, int start, int finish){
    }

    /* (5) compare string1 n string2, if string1 is lexicographically
    less than string2, return an integer <0. greater then interger>0,
    the same return 0 */
    int compare(String string1, String string2){
    }

    /* (6) concantenate string1 and string2 and return a ptr to the new String */
    String concat(String string1, String string2){
    }

    /* (7) return the length of string, "programming" return 11 */
    int length(String string){
    }

    /* (8) return a ptr to a C-style string containing a copy of string */
    char *StringChars(String string){
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This sounds like homework, so I'll give you the string copying function and you can use that to figure out the others.
    Code:
    char *copystr ( char *dst, const char *src )
    {
      char *s = dst;
      while ( ( *s++ = *src++ ) != '\0' );
      return dst;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    copystr

    but the whole point of this is that i should NOT use
    standard string handling functions.
    is [copystr] different from [strcpy]?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is [copystr] different from [strcpy]?
    Yes, copystr does the same thing but isn't a standard function, I wrote it myself as I was posting.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string & eof handling problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 11:46 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  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