Click to See Complete Forum and Search --> : printing char array
ct_eubanks
February 11th, 2003, 02:16 PM
I am trying to print a char array with an * between each character. I am getting my input from the user and since I can not determine the length of the string I am unsre how to output the string with the correct number of astricks.
any help is greatly appreciated
Runt888
February 11th, 2003, 02:24 PM
strlen will give you the number of characters. Try this:
for( int x = 0; x < strlen( myArray ); x++ )
cout << myArray[x] << "*";
cout << endl;
Kelly
ct_eubanks
February 11th, 2003, 02:56 PM
Hot dog.. That was it. I guess that is what I was supposed to learn out of that exercise. How to use strlen.
I have created 2 other little programs to play with strlen and it is pretty cool.
Thanks alot bud.
PaulWendt
February 11th, 2003, 03:00 PM
It looks like you're not at this point yet, but you could also use
the STL:
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main(int argc, char* argv[])
{
char theArray[] = "This is the array";
copy(&theArray[0], &theArray[strlen(theArray)], ostream_iterator<char>(cout, "*"));
cout << endl;
return 0;
}
Whether the above is easier to understand than the loop
approach is debatable :)
--Paul
ct_eubanks
February 11th, 2003, 03:51 PM
Looks a little confusing right now but thanks for the reply..
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.