Insert char into string?

This is a discussion on Insert char into string? within the C++ Programming forums, part of the General Programming Boards category; How can I insert characters into a string? Example: Code: {R5;;;;1;;1;} ---> {R5;0;0;0;1;0;1;}...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Insert char into string?

    How can I insert characters into a string?
    Example:
    Code:
    {R5;;;;1;;1;} ---> {R5;0;0;0;1;0;1;}

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    If I´m not wrong it´s like this

    Code:
    int main()
    {
         char aname[] = "Steve";
         string name = aname;
         cout << name << endl;
    return 0;
    }

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Or do you mean one character at time???

    Code:
    int main()
    {
         char aname[] = "Steve";
         string name;
    	 int size = sizeof(aname) / sizeof(char);
    	 for (int i = 0; i < (size -1); i++)
    		 name +=aname[i];
    
    	 cout << name << endl;
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    @ ripper079:
    I wanted to convert "C1;;;4;;1;" into something that looks like this string: "C1;0;0;4;0;1;".
    I tried strtok() but it just removes the ";" and returns: "C1 4 1".
    Is there a way to read the string up to a certain character (in this case ";")? After this I need some function to insert the "0" after this position.
    Any Idea?

    Thank You.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wanted to convert "C1;;;4;;1;" into something that looks like this string: "C1;0;0;4;0;1;".
    This is hasty, but it should work:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    template <typename Container, typename ValueType>
    Container insert_val ( Container orig, ValueType val, ValueType rep )
    {
      std::vector<char> dest;
      std::vector<char>::iterator it = orig.begin();
    
      while ( it != orig.end() ) {
        dest.push_back ( *it );
        if ( *it == val && *(it + 1) == val )
          dest.push_back ( rep );
        ++it;
      }
    
      return Container ( dest.begin(), dest.end() );
    }
    
    int main()
    {
      std::string broken = "C1;;;4;;1;";
    
      std::string fixed = insert_val ( broken, ';', '0' );
    
      std::cout<< broken <<std::endl;
      std::cout<< fixed <<std::endl;
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Works great. Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 03:29 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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