How to display individual character using string?

I

Immortal Nephi

I put number of characters together into string like this below.

#include <string>
#include <iostream>
using namespace std;

int main()
{
string s1 = "The cat is cute."
cout << s1 << endl;

// how to use loop to print each character individual between space?

return 0;
}

Output
The cat is cute.

T h e c a t i s c u t e.

Thanks...
 
H

Harald Finster

Hello


Immortal said:
// how to use loop to print each character individual between space?

please find a few suggestions below.


Greetings Harald




#include <string>
#include <iostream>
using namespace std;


//
// required in for_each below
// for suggestion2 below
//
void print( char c) {
cout << c <<" ";
}


//
// define a function object
// for suggestion3 below
//
//
class WhiteSpaceSeparatedPrinter : public unary_function<char,void> {
public:
void operator() (char c)
{
cout << c << " ";
}
};


int main()
{
string s1 = "The cat is cute.";
cout << s1 << endl;

// how to use loop to print each character individual between space?

//
// suggestion1:
// using a loop
//
for( unsigned int i=0 ; i<s1.length() ; ++i ) {
cout << s1 << ' ';
}
cout << endl;

//
// suggestion2:
// better: using for_each
// use the 'print' method defined above is executed for each
// character of the string which can be regarded as a
// sequence of chars
//
for_each( s1.begin(),s1.end(),print);
cout << endl;

//
// suggestion3:
// like suggestion2 but
// more object oriented style:
// use a function object instead of the global function
// operator() of WhiteSpaceSeparatedPrinter is executed
// for each character in the string
//
WhiteSpaceSeparatedPrinter whiteSpaceSeparatedPrinter;
for_each( s1.begin(), s1.end(), whiteSpaceSeparatedPrinter );
cout << endl;

// you could probably also use a stream_iterator in combination
// with a function

return 0;
}
 
A

Alan Woodland

Harald said:
Immortal said:
// how to use loop to print each character individual between space?
[snip]> //
// suggestion2:
// better: using for_each
// use the 'print' method defined above is executed for each
// character of the string which can be regarded as a
// sequence of chars
//
for_each( s1.begin(),s1.end(),print);
cout << endl;

//
// suggestion3:
// like suggestion2 but
// more object oriented style:
// use a function object instead of the global function
// operator() of WhiteSpaceSeparatedPrinter is executed
// for each character in the string
//
WhiteSpaceSeparatedPrinter whiteSpaceSeparatedPrinter;
for_each( s1.begin(), s1.end(), whiteSpaceSeparatedPrinter );
cout << endl;

// you could probably also use a stream_iterator in combination
// with a function

Better still:

#include <string>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
string s1 = "The cat is cute.";
cout << s1 << endl;

copy(s1.begin(), s1.end(), ostream_iterator<char>(cout, " "));

return 0;
}

Alan
 
J

James Kanze

Harald said:
Immortal Nephi wrote:
// how to use loop to print each character individual between space?
[snip]> //
// suggestion2:
// better: using for_each
// use the 'print' method defined above is executed for each
// character of the string which can be regarded as a
// sequence of chars
//
for_each( s1.begin(),s1.end(),print);
cout << endl;
//
// suggestion3:
// like suggestion2 but
// more object oriented style:
// use a function object instead of the global function
// operator() of WhiteSpaceSeparatedPrinter is executed
// for each character in the string
WhiteSpaceSeparatedPrinter whiteSpaceSeparatedPrinter;
for_each( s1.begin(), s1.end(), whiteSpaceSeparatedPrinter );
cout << endl;
// you could probably also use a stream_iterator in combination
// with a function
Better still:
#include <string>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
string s1 = "The cat is cute.";
cout << s1 << endl;
copy(s1.begin(), s1.end(), ostream_iterator<char>(cout, " "));
return 0;
}

Which outputs an extra space at the end. (It would be nice if
there were a variant of ostream_iterator which generated
separators correctly, but there isn't.) One could add a test
and use the ostream_iterator for all but the last character,
something like:

if ( ! s1.empty() ) {
std::copy( s1.begin(),
s1.end() - 1,
std::eek:stream_iterator< char >( std::cout, " " ) ) ;
std::cout << *(s1.end() - 1) ;
}

But that still wouldn't meet the stated requirement: "use a loop
to print each character[...]". (Of course, such a requirement
clearly indicates homework.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top