0

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.

8
  • Take a look at strncpy. However, you should try to use std::string instead and standard algorithms. Commented Jan 28, 2016 at 1:47
  • is there a way to get strncpy to start taking the array after n? or will it only do before n? Commented Jan 28, 2016 at 1:51
  • Before you start figuring out the algorithm, you might want to fix the obvious bug in the shown code that will lead to undefined behavior, memory corruption, and random crashes (hint: strlen() doesn't count the trailing '\0' that strcpy() adds). Commented Jan 28, 2016 at 1:55
  • Think about what an array is. It decays to a pointer, so you can increment the pointer to point to the middle of the array. Commented Jan 28, 2016 at 1:56
  • @Tremors You can use &string[n] to start copying from n. Commented Jan 28, 2016 at 2:01

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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;
}

1 Comment

This is more of a c style answer (using <cstring>) than a cpp one. I agree with @jalomas7 and would personally use <string> to solve this.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.