Hello. I have a following verse:

A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.

And I have to modify this text so that all lines ended in the same position.
Insufficient number of positions in the string complement using the spaces. These spaces must be divided evenly
I know that my code is very bulky, but I must use a "struct in my code"

How can I find the longest string and add spaces in others to perform the task?
Thanks!

#include "stdafx.h"
#include "iostream"
#include <string.h> 
using namespace std;

struct VERSE {
    char row_one[25];
    char row_two[25];
    char row_three[25];
    char row_four[25];
    char row_five[25];
    char row_six[25];
};

int _tmain(int argc, _TCHAR* argv[])
{
    struct VERSE v;
    strcpy_s(v.row_one, "A swarm of bees in May");
    strcpy_s(v.row_two, "Is worth a load hey;");
    strcpy_s(v.row_three, "A swarm of bees in June");
    strcpy_s(v.row_four, "Is worth a silver spoon;");
    strcpy_s(v.row_five, "A swarm of bees in July");
    strcpy_s(v.row_six, "Is hot a worth a fly.");
    cout << v.row_one << endl << v.row_two << endl << v.row_three << endl
        << v.row_four << endl << v.row_five << endl << v.row_six << endl;

    cout << strlen(v.row_one) << endl;
    cout << strlen(v.row_two) << endl;
    cout << strlen(v.row_three) << endl;
    cout << strlen(v.row_four) << endl;
    cout << strlen(v.row_five) << endl;
    cout << strlen(v.row_six) << endl;

    //the length of row
    /*
    int length = 0;
    for(int i = 0; v.row_two[i] != '\0'; i++) {
        length++;
    }
    printf("Length of second row is: %d\n", length);
    */

    return 0;
}

You'll need to find two things: maximum line length and the number of tokens on each line.

One you have those, the algorithm might looks something like:

for each line:
   if line.length == largest:
      print line
   else:
      extra_spaces = largest - line.length
      for token in line and extra_spaces > 0:
         add space to beginning of token
         extra_spaces = extra_spaces - 1
      print line

I would suggest you use std::string instead of char[] since std::string does memory management and resizing for you and will make this excersize much easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.