Function that adds "!" to end of String

This is a discussion on Function that adds "!" to end of String within the C++ Programming forums, part of the General Programming Boards category; Can someone tell me how to write code for a function that adds "!" to the end of each STRING ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    Function that adds "!" to end of String

    Can someone tell me how to write code for a function that adds "!" to the end of each STRING object in a list. This is what I have:
    Code:
    void exclamation(Node* head)
    {
      const string s = "!";
    string list +=s;
    cout << list;
    }
    Will this work?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I think it will.
    but I think you can only do this:
    Code:
        List = List + "!";
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    No - you haven't done anything at all to any string pointed to by head.

    All you are doing here is creating 2 strings, one called s and one called list, both containing !, and then printing ! to the screen.

    You need to find a way to use that pointer to walk through the list of strings one element at a time, adding '!' to each one. (You don't even need that new string s. The right operand of += can be a char.)

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    15
    What about using strcat()?

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    while (head != NULL) {
      head->str += "!";
      head = head->next;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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