Znap
Znap

Reputation: 103

iterate through string and combine the previous characters in each loop

In the string "61C99161" I want to iterate through each character and combine the previous looped characters. For example in this code:

string hex = "61C99161";

for (std::string::size_type i = 0; i < hex.size(); ++i) {

    cout<<hex[i]<<endl;
}

It prints:

6
1
C
9

...and so on. But how can I print the following?

6
61
61C
61C9

Upvotes: 1

Views: 116

Answers (7)

Kerrek SB
Kerrek SB

Reputation: 477358

Just to add a variation to the mix:

for (std::size_t i = 0; i != hex.size(); ++i)
{
    std::cout.write(hex.data(), i + 1);
    std::cout.put('\n');
}

Upvotes: 3

Rama
Rama

Reputation: 3305

Other version could be simply using another string (buffer) to store the characters printed before as in the following example:

#include <iostream>
using namespace std;

int main() {
    string hex = "61C99161";
    string buffer;

    for (const char& c : hex) //Range-based for loop
    {
        std::cout << buffer << c << endl;
        buffer = buffer + c;
    }
    return 0;
}

Upvotes: 0

mindthegap
mindthegap

Reputation: 363

Try something like:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  std::string hex = "61C99161";
  std::ostringstream ss;
  for (std::string::size_type i = 0; i < hex.size(); ++i) {
    ss << hex[i];
    std::cout << ss.str() << std::endl;
  }
}

If you have an array of std::strings:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  unsigned const S = 2;  // No magic constants...
  std::string hex[S] = {"61C99161", "g53fb4h6"};
  // I would rather use std::vector<std::string> by the way:
  // std::vector<std::string> hex{"61C99161", "g53fb4h6"};

  std::ostringstream ss;
  for (unsigned i = 0; i < S; ++i) {
    for (unsigned j = 0; j < hex[i].size(); ++j) {
      ss << hex[i][j];
      std::cout << ss.str() << std::endl;
    }
    ss.str("");  // Remove this line if you want to concatenate all the words
  }
}

Upvotes: 2

Pete Becker
Pete Becker

Reputation: 76428

You need to keep track of where you are in the string, and write everything up to that point:

int main() {
    std::string hex = "61C99161";
    for (int offset =  1; offset < hex.size(); ++offset) {
        for (int i = 0; i < offset; ++i)
            std::cout << hex[i];
        std::cout << '\n';
    }
    return 0;
}

Upvotes: 2

AAryan
AAryan

Reputation: 20140

You can do in this way :

#include <iostream>
#include <string>

using namespace std;

int main()
{

  string hex = "61C99161";

  for (int i=0; i<hex.size(); ++i)
    cout << hex.substr(0, i) << endl;

}

Upvotes: 0

thehulinsky
thehulinsky

Reputation: 9

When you print a character, add it to a previouslyPrinted string, which will be printed before every character.

std::string previouslyPrinted="";
string hex = "61C99161";

for (std::string::size_type i = 0; i < hex.size(); ++i) {

    cout<<previouslyPrinted<<hex[i]<<endl;
    previouslyPrinted+=hex[i];
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490338

There are quite a few possibilities. One obvious one would be something like:

for (i=0; i<hex.size(); ++i)
    std::cout << hex.substr(0, i) << "\n";

Upvotes: 3

Related Questions