Thread: dynamic strings

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    ?

    I wasn't posting a new topic it was a reply to the above problem. he had a word in this case the name of a variable and he wanted to replace all instances of it in his string even though he set the value before he set the value of the the replacement string. I said that he should try regular expressions and cout the result. I'm sorry if I was somehow unclear.

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    Ok, I want a C++ equivalent of the following C code:


    Code:
    #include <stdio.h>
    
    int main()
    {
       char * location;
       char * my_string = "The rain in %s is falling on the plain";
       location = "Spain";
       printf (my_string, location);  // The rain in Spain is falling on the plain
    
       return 0;
    }

    That should answer all of my questions.



    Thank you!


    Ham
    Last edited by Ham; 12-16-2005 at 05:21 PM.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <cstdio>
    using namespace std;
    
    int main()
    {
       char * location;
       char * my_string = "The rain in %s is falling on the plain";
       location = "Spain";
       printf (my_string, location);  // The rain in Spain is falling on the plain
    
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    another one
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main() {
       string location;
       string my_string = "The rain in %s is falling on the plain";
       location = "Spain";
       int p = my_string.find("%s");
       my_string.replace ( p, 2, location );
       cout << my_string << endl;
       return 0;
    }

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    treenef's code in the fourth post in this thread is functionally equivalent. I will assume that the difference is that you want to have my_string initialized first, followed by location later. The best way to do that depends on why you want to do that. In this simple example, there is no reason to do that, which is why treenef's code should be sufficient.

    However, here are two C++ examples that assign the location after the my_string is initialized.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string location;
        string my_string1 = "The rain in ";
        string my_string2 = " is falling on the plain";
    
        location = "Spain";
    
        cout<<my_string1 + location + my_string2;
        return 0;
    }
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string location;
        string my_string = "The rain in  is falling on the plain";
        int insertLoc = 12;
    
        location = "Spain";
        my_string.insert(insertLoc, location);
    
        cout<<my_string;
        return 0;
    }
    There are other options as well.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    #include <boost/format.hpp>
    
    int main()
    {
        boost::format fmt("The rain in %s is falling on the plain.");
    
        std::string location = "Spain";
    
        std::cout << fmt % location;
    }
    http://www.boost.org/libs/format/doc/format.html
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fwrite can't write dynamic strings???
    By Devil Panther in forum C Programming
    Replies: 26
    Last Post: 05-19-2009, 02:18 AM
  2. Strings and Dynamic Memory
    By grooveordie in forum C Programming
    Replies: 3
    Last Post: 10-20-2008, 02:08 PM
  3. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. Inputing strings into a dynamic array
    By Nakeerb in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2002, 02:38 AM