repeat char

hi, i'm new at C :s

is there a way to print one char for n times without using for or similar?

thanks
Probably, but why would you? The definition of "for" is pretty much "a loop for when you want to repeat something n times".

If it's just a challenge, you could try recursion, but that's pretty silly for this thing.
goto :D

1
2
3
4
5
6
7
int i = 0;

lab_start:
i++;
cout << c << endl;
if(i < 10)
  goto lab_start;

But that's a really dirty way to code program logic ;o)
Last edited on
That's a for loop without the actual keyword, you cheater!
1
2
3
4
5
6
int i = 10;
char *c = new char[i+1];
memset (c, 'a', i);
c[i] = '\0';
printf("%s", c);
delete c;


The requirement is to only "print" ...
Since this is a C++ forum, a couple of C++ approaches:

1
2
3
4
5
6
7
8
9
#include <string>
#include <iostream>
int main()
{
    int n = 10;
    char c = 'z';

    std::cout << std::string(n, c) << '\n';
}


or

1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <iostream>
#include <iterator>
int main()
{
    int n = 10;
    char c = 'z';

    fill_n(std::ostream_iterator<char>(std::cout), n, c);
    std::cout << '\n';
}
Topic archived. No new replies allowed.