Tremors
Tremors

Reputation: 133

How to split an array using strings?

I need to write a program that prompts the user to input a string, then determine the middle of the string, and generate a new string which swaps the two halves of the string and then output the results.

So far I have

int main(void) {

char *string = NULL;
char temp[1000];
cout << "Please enter a string" << endl;
cin.getline(temp, 999);
int length = strlen(temp);
string = new char[length];
strcpy(string,temp);
length = length / 2;

    return EXIT_SUCCESS;
}

Which takes in the string and stores it. I just need a way to move that second half to a new array and I know I need to use strcpy() but I don't know how to properly reference that portion of the array.

Upvotes: 0

Views: 142

Answers (4)

Blastfurnace
Blastfurnace

Reputation: 18652

Since this is C++ I'm going to suggest a standard library algorithm. You're asking to swap two halves of a sequence and std::rotate does just that. Unfortunately it does the rotation in-place and you want the result in a different string.

You could copy the string and then do the rotation but there is a std::rotate_copy algorithm that will do both (and faster than separate copy/rotate steps).

Example with char arrays:

#include <algorithm>
#include <cstring>
#include <iostream>

int main()
{
    char text[1000], result[1000];
    std::cout << "Please enter a string\n";
    std::cin.getline(text, 999);
    size_t length = strlen(text);

    std::rotate_copy(text, text + length / 2, text + length, result);
    result[length] = '\0';

    std::cout << text << '\n' << result << '\n';
}

Example with std::string:

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string text, result;
    std::cout << "Please enter a string\n";
    std::getline(std::cin, text);
    size_t length = text.size();

    result.resize(length);
    std::rotate_copy(text.begin(), text.begin() + length / 2, text.end(), result.begin());

    std::cout << text << '\n' << result << '\n';
}

Demo on ideone.com

You could possibly use std::swap_ranges but that assumes both ranges are the same size.

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76498

std::string text(whatever...);
int sz = text.size() / 2;
for (int i = 0; i < sz; ++i)
    std::swap(text[i], text[sz + i]);

This might be off by one when text.size() is odd.

Upvotes: 0

Vicente Cunha
Vicente Cunha

Reputation: 204

You were half way through the solution. Here I finished it using strncpy to get the first half and pointer incrementation to get the second one.

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main(void)
{
    char temp[1000];
    cout << "Please enter a string" << endl;
    cin.getline(temp, 999);
    int length = strlen(temp);

    char firstHalf[512];
    strncpy (firstHalf, temp, length/2);
    cout << "firstHalf: " << firstHalf << endl;

    char* secondHalf = temp + length/2; 
    cout << "secondHalf: " << secondHalf << endl;

    char* swapped_str = strcat(secondHalf, firstHalf);  
    cout << "Swapped string: " << swapped_str << endl;

    return EXIT_SUCCESS;
}

Upvotes: 0

jalomas7
jalomas7

Reputation: 412

if you are trying to use C, use strncpy. However, I recommend using C++ std::string and using the std::string.substr() and concatenation. The latter would be easier at least to me.

Upvotes: 0

Related Questions