Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create string of specific length in C++

Tags:

For my class, I have to take three strings, then center them.

Here's a link to the problem, and no, I'm not asking for the answer to the problem!
http://www.hpcodewars.org/past/cw3/problems/Prog05.htm

I have everything I need, but I need to create a string of "*" with a specific length. In this case, it needs to be 21 characters long of asterisks, and I don't know how to create that.

I mean, yeah, I can do

string test = "********************"

but it needs to be a different length as it changes.

I have a variable set to how long the string needs to be, but I need to know how to create a string with a specific length, then add in the asterisks.

Code so far:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string line;
    string lines[2];
    int x = 0;
    int maxLength;
    ifstream myfile ("example.txt");

    if (myfile.is_open()) // opening file, setting the strings in the input stream to a variable to use at a later time
    {
        while ( getline (myfile,line) )
        {
            lines[x] = line;
            x++;
        }
        myfile.close();
    }

    if(lines[0].length() > lines[1].length()) //finding the max length;
    {
        maxLength = lines[0].length();
    }else
    {
        maxLength = lines[1].length();
    }
    if(lines[2].length() > maxLength)
    {
        maxLength = lines[2].length();
    }

    maxLength = maxLength + 4;

    cout<<maxLength<<endl;

    return 0;
}
like image 976
Brenden Rodgers Avatar asked Dec 08 '16 01:12

Brenden Rodgers


People also ask

How do you create a string for a specific length?

The idea is to first, get the length L of the string is taken as input and then input the specific character C and initialize empty string str. Now, iterate a loop for L number of times. In every iteration, the specific character is concatenated with string str until the for loop ends.

Which create a string of variable length?

To create a string of variable length:Use the Array() constructor to create an array of empty elements with length N + 1. Call the join() method on the array to join the elements with the string you want to repeat. The join method returns a new string, where the array elements are separated by the specified string.

Can you change the length of a string in C?

C (unlike C++, BTW) doesn't have a simple mechanism for managing strings of varying lengths. The C string facilities, in some cases, are what higher-level mechanisms are built on top of. Show activity on this post. Short answer is no.

How do you get a length of a string C?

Use the strlen() function provided by the C standard library string. h header file. char name[7] = "Flavio"; strlen(name); This function will return the length of a string as an integer value.


1 Answers

It's much simpler than you think. std::string has a constructor just for this purpose:

#include <string>
#include <iostream>

int main()
{
    std::string s(21, '*');

    std::cout << s << std::endl;

    return 0;
}

Output:

*********************
like image 159
Sam Varshavchik Avatar answered Sep 26 '22 22:09

Sam Varshavchik