Concatinate strings located inside a vector.

This is a discussion on Concatinate strings located inside a vector. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, first off i'm trying to Write a program that reads a series of strings into a vector and ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Question Concatinate strings located inside a vector.

    Hi guys, first off i'm trying to Write a program that reads a series of strings into a vector and then prints out a string with all elements concatenated, with each element separated with a "-". So if the input is "hello world" then your proram should output "hello-world".


    This program doesn't seem too hard at first glance and below is my current progress. I'm only on chapter 3 of my book so this program shouldnt be overly complicated. What is getting me confused it the book does a poor job explaining how to treat and munipulate the data inside the vector. Once the series of strings is loaded into the vector can we still treat the data as if they were strings.

    My thought process behind this would be the same way as I would replace printable whitespace with a "-" character by going through the data located within the string one char at a time but that doesn't seem to be working.

    I guess my real question is what is the best way to treat the strings located inside the vector. Any help or suggestions is greatly appreciated.

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <vector>
    
    using std::cin ;
    using std::cout;
    using std::string;
    using std::vector;
    
    int main(){
    
      vector<string> text() ;  //empty vector
    
      cout << "Please enter a string" ;
    
      while( cin >> text){    //append word to text
        text.push_back(word);
    
    // This is the part thats throwing me for loop  below (no pun intended)
    
      for( vector<string> :: size_type index = 0 ; index != text.size(); ++index)
        {
          if ( isspace(text[index])){
            cout << "-" ;
    
          }else {
            cout << text[index] ;
          }}

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    text is a vector of strings,so when you call text[index] you're retrieving an entire string, not a single character.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I would replace printable whitespace with a "-" character

    There will be no printable whitespace in the strings you are storing. This is because when you read in strings with cin >>, the whitespace is skipped.

    That's actually a good thing here, and will make your job easier. Instead of finding whitespace to replace with the '-', all you have to do is add a '-' after each string in the vector as you concatenate them together.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Yay I got it working and I think its doing everything required. Thank you two for all your help.

    Here is the solution I came up with.


    Code:
    int main(){
    
      string w ;
      vector<string> text ;  //empty vector
    
      cout << "Please enter a string or type 'eof' when finished entering strings : \n" ;
    
      while( cin >> w && w != "eof" ){    //append word to text
          text.push_back(w);
        }
    
      for( vector<string> :: size_type index = 0 ; index != text.size(); ++index)
        {
          if(index == text.size()){
            break;
          }
          else if(index == text.size() -1 ){
            cout << text[index] ;
              }
          else {
            cout <<text[index] << "-" ;
          }}

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Looks good.

    The only thing I'd say is that the first part of the if inside the for loop shouldn't be necessary. The loop won't be entered if index is equal to text.size().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Accessing Member Methods inside a Vector
    By xmltorrent in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2008, 12:02 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Putting strings from a file into a vector
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 08:15 AM

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