Thread: Can repeated characters be printed with a C statement?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Can repeated characters be printed with a C statement?

    'Basic' had a way to choose a character and repeat it as many times as needed in one statement. Great for drawing horiziontal lines in text-mode applications. Does such a statement exist in C of should I just use a FOR/LOOP? (No need to post a FOR/LOOP code, I can do that, I'm just looking for a shortcut if it exists by using some form of printf, puts, etc. statement if ones available.)

    Thanks,

    Pete

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In C? Not that I know of using just a single statement. You can do it in C++ by outputting a temp string object initialized to an arbitrary number of a specific character though.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write a function.
    Code:
    void putcn( int c, size_t n )
    {
        size_t x;
        for( x = 0; x < n; x++ )
            putc( c );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    An odd thing to do:
    Code:
    #include <stdio.h>
    
    void foo(int count)
    {
       static const char ch[] = "*************************************************";
       printf("%.*s\n", count, ch);
    }
    
    int main()
    {
       foo(5);
       foo(10);
       return 0;
    }
    
    /* my output
    *****
    **********
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    9
    Thank you everyone,

    I did not think I missed anything. I spent about an hour reading over various C statements and tutorials before I asked.

    I am working strictly in C to avoid getting too confused but thanks hk_ for the C++ info.

    quzah - The FOR/LOOP I know, but I did mine with printf. Thank you for posting it another way with put. Examples alway help a lot.

    Dave - Good idea, glad I thought of it (well, after I saw your post!) Sure, that would work great for the times when you have text characters that form lines and borders. Thanks for showing me how to control the size of the output. Very much appreciated.

    Wow, I'll say it again, what a great forum community you have here. I also need to apologize. I left my computer on today when I left to attend to some other things. I didn't shut down the browser windows, so it may have read as if I was on. Sorry about that. I would have responded sooner had I been able to but I think you will find I will be the type to always respond to a reply...well, unless it's ancient.

    Thanks again,

    Pete

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  2. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  3. Funny characters being printed
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-17-2005, 09:02 AM
  4. International (scandinavian) characters?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2002, 06:14 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM