First lets take care of your errors.
1) You need to use
int main()
not
void main()
. The main function always returns a integer so always declare it as int. If your compiler doesn't give you a error or warning for that switch to a standard compliant compiler.
2)
string things = burp;
Is in error to assign a string literal to a std::string you need to enclose the literal in
"Literal Here".
3) You have a memory leak on line 9
char *a=new char[things.size()+1];
you use new but never use delete. Always remember that whenever you call new you always need to call delete unless a classes destructor handles it for you.
Now lets move onto your question. To print a string's characters you can index a certain character like so
things[0]
will print the first character.
Or if you want to print out a range of characters from the string you can use the
string.substr()
function which
std::string
provides.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
string things = "burp";
cout << things << endl;
cout << things[0] << endl;
Sleep (1000);
cout << things[1] << endl;
Sleep (1000);
// Prints every character from things[2] till the end of the string
cout << things.substr(2) << endl;
}
|
That should give you all the tool you need :) All you got to do is figure out how to use them to do what you want.
I would also recommend you use C++ techniques that are less error prone then C techniques. Meaning use the C++ features like vectors and strings instead of char arrays. I'm not sure why you need to use C strings in this situation since you are already using std::string and that provides all the functionality you will need.