#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14

    My Sentence class


    Hi there everyone,
    I am working on a hw assignment for one of my programming classes. I am designing a Sentence class that manipulates a static character array of 200. I am supposed to manipulate the data with certain operations and I need help on the Append(char *s) method. I can append just fine but the problem arises when a user appends more characters than the alloted array size. I also have to take into account any strings that are already in the character array.
    Here is my header file and Append function:
    Code:
    #ifndef SENTENCE_H
    #define SENTENCE_H
    
    #include <iostream>
    #include <cstring>
    
    // sets number of elements string array can have to constant for easy modification
    const int SIZE = 200;
    
    class Sentence
    {
     public:
      Sentence();
      Sentence(char *s);
      void Input();
      char *GetSentence();
      void Append(char *s);
      int Size();
      void Clear();
      int NumWords();
      void Insert(char *s, int index);
      void Delete(int index, int num_chars);
     private:
      char string[SIZE];
    };
    
    #endif
    Code:
    void Sentence::Append(char *s)
      // Appends a string (given in parameter) to the end of the stored string
    {
      int i = 0;
      // a similar example is the first example in the online classroom notes(Ex. 05_21.cpp)
      while(string[i] != '\0')
        i++;
    
      // check if the string length of the incoming string plus existing size is equal to
      //
      // Secondly, we concatenate whatever the string s has into the character array
      for (; string[i] = *s; i++,s++)
        {}// empty statement
    }
    So, if the user does try to append more characters than there is space, for I need to output an error message. I know I can user the strlen() function but in my Deitel book it does not explain the strlen thoroughly enough. If there are any questions or if anything is unclear, please let me know. Thank you.
    Last edited by supaben34; July 12th, 2003 at 09:41 AM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Code:
    void Sentence:Append(char *s) {
        if (strlen(string) + strlen(s) > SIZE -1) {
            // We're comparing to SIZE - 1 cuz we need
            // 1 byte for the NULL terminating char
            cout << "String too long" << endl;
            // U can also use throw("String too long"); instead, but I don't
            // know if you've discussed exceptions yet.
        } else {
            // Append s to string
            strcat(string, s);
        }
    }
    To optimize this code, you might want to keep track of the current size of the string, every time you insert, append or delete from it. I see you've already declared a member variable called size, presumably for this purpose. This will save you the trouble of recomputing strlen(string). HTH :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14

    Help on Insert() function


    Hi there,
    I was wondering if anyone out there could help a student one more time with the Insert() function. By the way Scorpions4ever, thank you for helping me with the append(). This is what I have for the Insert() function so far. Please take a look at it and all feedback will be greatly appreciated. By the way, this function is related to the same header file up top.
    Code:
    void Sentence::Insert(char *s, int index)
      // inserts a string(given in parameter) into the currently stored string, 
     // first parameter is string to be inserted, second parameter is the index of the array where it will be inserted(if possible)
    {
      int size_of_string = Size();
      int length = strlen(s);
      int sum = size_of_string + length;
    
      if (sum > SIZE)
        // if there is not enough room to insert, output error message
        cout << "** Insert Error: not enough room **" << "\n";
      else if ((index > (size_of_string+1)) || index < 0)
        // else if the index is an invalid number(i.e. - not contigous with array or negative number), output error message
        cout << "** Insert Error: Illegal insert point **" << "\n";
      else{
        // insert character string
    
        // first move the existing characters each one space to the
        // right starting at furthest array element and working back to index
        for (int i = size_of_string; i >= index; i--)
          string[i] = string[(i+1)];
    
        // Next, place incoming string into our string array
        int i = 0;
    
        while (i <= length){
          string[index] = *s;
          index++;
          i++;
        }
    
        // at this point, all characters must be inserted in right order
      }
    }
    Thanks again.
    Last edited by supaben34; July 12th, 2003 at 11:19 AM.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Spotted some errors right off the bat. Bold lines are my corrections. HTH :)
    Code:
    // first move the existing characters each one space to the
        // right starting at furthest array element and working back to index
        for (int i = size_of_string; i >= index; i--)
          // string[i] = string[(i+1)];
          string[i] = string[i-1]; // Have to work backwards
    
        // Next, place incoming string into our string array
        int i = 0;
    
        while (i <= length){
          //string[index] = *s;
          string[index] = s[i];
          index++;
          i++;
        }
    DISCLAIMER: I haven't actually tested the code.
    Last edited by Scorpions4ever; July 12th, 2003 at 11:25 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14
    Your absolutely right! wow, how did I let that one pass by me. Thank you. One other thing how can you give the pointer s array brackets? It is apparently legal but why?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14

    Insert function not correct


    You know what Scorpions? the Insert function is not correct I mean it inserts but the rest of the string does not go in there.
    For example if your input was:
    Code:
    John Smith
    and your function is
    Code:
    Insert("blah", 3)
    it should say
    Code:
    Johblahn Smith
    but instead its saying:
    Code:
    Johblah
    Any ideas?
    Last edited by supaben34; July 12th, 2003 at 11:42 AM.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    >> One other thing how can you give the pointer s array brackets?
    Pointers and arrays are mostly interchangable in C/C++. See http://www.eskimo.com/~scs/C-faq/s6.html

    >>but instead its saying:
    >>code:
    >>Johblah
    >>Any ideas?
    Yeah, chances are you're copying the null terminator from s into string as well. Try changing this line from:
    while (i <= length){
    to:
    while (i < length){
    and see if that fixes it
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14

    Delete function


    My last function is the Delete function. This is what I have so far. The first parameter is where the function should start "deleting" and the second parameter is the num_chars(moving to the right) that it will delete. My code deletes the last character always!! Scorpions if you're out there I could sure use some of your help. I will be very grateful. Thank you.
    Code:
    void Sentence::Delete(int index, int num_chars)
      // deletes characters from the stored string
    {
      int size_of_string = Size();
    
      if (index > (size_of_string-1))
        cout << "** Delete Error: Attempt to delete past end of string" << "\n";
    
      else if (num_chars < 0)
        cout << "** Delete Error: Cannot delete a negative number of characters" << "\n";
    
      else {
        // deletion
    
        // move farthest non-deleted element to index deleting everything in between
        while (num_chars < 0){
          string[(size_of_string-1)] = string[size_of_string];
          num_chars--;
          size_of_string--;
        }
    
        // Lastly, null everything else in the string
        int null_chars = index + num_chars;
        for (int i = null_chars; i < SIZE; i++)
          string[i] = '\0';
      }
    
    }
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Code:
    else {
      // deletion
    
      // move fartheset non-deleted element to index deleting everything in between
      for (int x = 0; x < numchars; x++)
        string[index + x] = string[index + numchars+x];
    
      // Now add nulls to everything else in the string
     int null_chars = index + num_chars;
        for (int i = null_chars; i < SIZE; i++)
          string[i] = '\0';
    }
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Tallahassee
    Posts
    55
    Rep Power
    14

    Problem catching an error


    Hello again,
    There seem to be a problem with my Insert() function. It does not correctly catch all of the error messages. I've checked it a dozen times and I can't find anything wrong. Would you please help me find the error
    Code:
    int Sentence::Size()
      // returns size of the stored string
    {
      char *s;
      s = string;
    
      int char_count;
      for ( char_count = 0; *s != '\0'; *s++ )
        ++char_count;
    
      return char_count;
    }
    Code:
    void Sentence::Insert(char *s, int index)
      // inserts a string(given in parameter) into the currently stored string
    {
      int size_of_string = Size();
      int length = strlen(s);
      int sum = size_of_string + length;
    
      if (sum > SIZE)
        // if there is not enough room to insert, output error message
        cout << "** Insert Error: not enough room. **" << "\n";
      else if ((index > size_of_string) || (index < 0))
        // else if the index is an invalid number(i.e. - not contiguous with array or negative number), output error message
        cout << "** Insert Error: Illegal insert point. **" << "\n";
      else{
        // insert
    
        // first move the existing characters starting at furthest array element and working back to index
        for (int i = size_of_string; i >= index; i--)
          string[(i+length)] = string[i];
    
        // Next, place incoming string into our string array
        int i = 0;
    
        while (i < length){
          string[index] = s[i];
          index++;
          i++;
        }
    
      }
    }
    I just gave you the Size() function just in case there is something wrong with that. Header file is the first post. Thank you so much.
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Your Size() function has an error in it -- you forgot to initialize char_count! Instead of doing this:
    int char_count;
    Try this instead:
    int char_count = 0;

    Also, why not just call strlen() within Size(), which does exactly what your code is doing.
    Last edited by Scorpions4ever; July 13th, 2003 at 03:14 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo