CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Posts
    7

    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

  2. #2
    Join Date
    Jun 2002
    Location
    Colorado, U.S.A.
    Posts
    980
    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

  3. #3
    Join Date
    Feb 2003
    Posts
    7
    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.

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  5. #5
    Join Date
    Feb 2003
    Posts
    7
    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
  •  





Click Here to Expand Forum to Full Width

Featured