|
-
February 11th, 2003, 03:16 PM
#1
printing char array
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
-
February 11th, 2003, 03:24 PM
#2
strlen will give you the number of characters. Try this:
Code:
for( int x = 0; x < strlen( myArray ); x++ )
cout << myArray[x] << "*";
cout << endl;
Kelly
-
February 11th, 2003, 03:56 PM
#3
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.
-
February 11th, 2003, 04:00 PM
#4
It looks like you're not at this point yet, but you could also use
the STL:
Code:
#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
-
February 11th, 2003, 04:51 PM
#5
Looks a little confusing right now but thanks for the reply..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|