16

I try to iterate through a string char by char. I tried something like this:

void print(const string& infix)
{
char &exp = infix.c_str();
while(&exp!='\0')
{
         cout<< &exp++ << endl;
    }
}

So this function call print("hello"); should return:

h
e
l
l
o

I try using my code, but it doesn't work at all. btw the parameter is a reference not a pointer. Thank you

jww
  • 97,681
  • 90
  • 411
  • 885
user1988385
  • 2,819
  • 4
  • 18
  • 17
  • The parameter might be a reference, yes, but so is `exp`. By the way, `std::string` has overloaded `operator[]`, and it has `begin()` and `end()` functions, as well as working with the free version of said function pair for iterating. – chris Feb 03 '13 at 00:52
  • possible duplicate of [How can I iterate through a string and also know the index (current position)?](http://stackoverflow.com/questions/1315041/how-can-i-iterate-through-a-string-and-also-know-the-index-current-position) – jww Oct 12 '14 at 04:54
  • possible duplicate of [For every character in string](http://stackoverflow.com/questions/9438209/for-every-character-in-string) – Csq Oct 12 '14 at 07:49

4 Answers4

28

Your code needs a pointer, not a reference, but if using a C++11 compiler, all you need is:

void print(const std::string& infix)
{
    for(auto c : infix)
        std::cout << c << std::endl;
}
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
15
for(unsigned int i = 0; i<infix.length(); i++) {
    char c = infix[i]; //this is your character
}

That's how I've done it. Not sure if that's too "idiomatic".

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
6

If you're using std::string, there really isn't a reason to do this. You can use iterators:

for (auto i = inflix.begin(); i != inflix.end(); ++i) std::cout << *i << '\n';

As for your original code you should have been using char* instead of char and you didn't need the reference.

David G
  • 94,763
  • 41
  • 167
  • 253
  • But, maybe he doesn't only want to copy it? He's simply asked how to iterate through the string character by character. – Dhaivat Pandya Feb 03 '13 at 00:55
  • Depending on what you're doing with it, there's probably something to replace the `copy` call, though, that works just about the same. – chris Feb 03 '13 at 00:56
  • The funny thing about the output is that you can and should just `std::cout << infix;`. However, needing to do anything else, an algorithm or iterator loop works. Also, if using `auto` already, I'd use a range-based for loop over an iterator loop as well :p – chris Feb 03 '13 at 01:04
0

std::string::c_str() returns const char*, you can't use char& to hold it. Also exp is pointer already, you don't need reference:

Better use iterator though:

void print(const string& infix)
{
  for (auto c = infix.begin(); c!=infix.end(); ++c)
  {
    std::cout << *c << "\n";
  }
  std::cout << std::endl;
}

To fix your original code, try:

void print(const string& infix)
{
  const char *exp = infix.c_str();
  while(*exp!='\0')
  {
    cout << *exp << endl;
    exp++;
   }
 }
billz
  • 44,644
  • 9
  • 83
  • 100
  • Is there any reason we would use char* over just a for loop (in my answer)? I've never been sure if that's how other people have done it. – Dhaivat Pandya Feb 03 '13 at 00:56
  • @DhaivatPandya, Not really, no. Iterating a `std::string` by using a `const char *` seems like a pointless endeavour to me. – chris Feb 03 '13 at 00:57