The basics of programming in c++ for beginners

A task: filling in the word line

There are some string (the word, number), for example “home”. It is necessary to fill in the word line (sequence of letters of the word) for a certain number of iterations.

For example: base line – “home”, the required number of characters results 11, результат: “homehomehom”

In this task, the main requirement should be: do it as many different ways! (the task itself is very elementary).

Solutions:

Here's how it will look in the performance:

Problems and solutions, c ++, programming practice

It shows several (5) solution options for the base line in a std format::string. But much more interesting options can be written for the string to char format[] (this code will be even easier to understand).

P.S. In this case, your options for function (if you want to store an array of functions-handlers) the prototype may have:

We offer the following options!

3 thoughts on “A task: filling in the word line

  1. The best optimality shown will be rep4, but there is a buffer allocation (char buf[1000]) made from the ceiling and purely for illustration, easy to overflow.
    I will add more 2 variation in the development of this method:

    string rep6( const string& base, uint rep ) {
    char* buf = (char*)alloca( rep * 2 );
    strcpy( buf, base.c_str() );
    while( strlen( buf ) < rep )
    memmove( buf + strlen( buf ), buf, strlen( buf ) + 1 ); // удвоить длину
    buf[ rep ] = '\0';
    return string( buf );
    }


    string rep7( const string& base, uint rep ) {
    char buf[ rep * 2 ];
    strcpy( buf, base.c_str() );
    while( strlen( buf ) < rep )
    memmove( buf + strlen( buf ), buf, strlen( buf ) + 1 ); // удвоить длину
    buf[ rep ] = '\0';
    return string( buf );
    }

  2. #include
    #include
    using namespace std;

    int main()
    {
    int a;
    cout<>a;
    int const BUFERSIZE=a;
    int bufer=0;
    string slovo;
    cout<>word;

    do
    {for(int i=0;i<slovo.size();i )
    {
    if(bufer<BUFERSIZE)
    cout<<word[i];
    bufer ++;
    }
    }while(bufer < BUFERSIZE);

    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *