Help! Homework using Strings

This is a discussion on Help! Homework using Strings within the C++ Programming forums, part of the General Programming Boards category; I think the problem describes exactly how the student is expected to solve it. It may not be a good ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the problem describes exactly how the student is expected to solve it. It may not be a good way, but it seems close to what OP originally had.

    Write a program that reads a string containing exactly four words, (separated by * symbols) into a single string object. Next, extract each word from the original string and store each word in a string object. Then concatenate the words in reverse order to form another string. Display both the original and final strings. (Hint: To extract the words, you should use the 'find' member function to find each symbol *, assign the characters up to the * to one of the four string objects, and then remove those characters from the original string.)
    As I understand the limitation are that they haven't learnt getline (hence the funny delimiter), and loops and containers (hence the four string objects).

    The funny thing is that you are supposed to destroy the original and yet display it (one might also use substr), but a lazy way around it is to display the original before you start destroying it. The output will be the same one way or another.

    I don't see a requirement to put in a new delimiter. For example, if I inputted "ant*bear*cobra*delta", accepted output might be "deltacobrabearant".

    The original warnings were about conversion from size_t to int. String method find returns a size_t (which is an unsigned type), you were storing it in an int. For extremely large strings an int may not be able to store a legal return value correctly. For small strings this problem won't happen but still you might fix this by declaring remove_1 etc as size_t.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Strictly speaking, they return a string::size_type, which usually is, but does not have to be, a typedef for std::size_t.

    Anyway, if you want to use size_t, you have to #include <cstddef> or <cstdlib>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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