0

Okay, so I need some help getting my string to swap around.

Here is the overall code of what I am trying to do, but I can't just move the string around. I started off trying to convert it to characters but the majority of replies said to just use the std::swap function, however I am really lost in using this...

My overall goal is to permute a string, which can be specified to a certain section of the string. I am new to C++, I am just unsure how to use C++ methods/functions in order to achieve this.

(there is also a main.cc and Permutation h. but its only for defining variables, skeletal code basically)

All help appreciated, I shall check back here in about 2 hours.

UPDATED CODE)

    #include <iostream>   // for cout
#include <cstdio>     // for printf()
#include <sstream>    // for stringstream
#include <stdio.h>
#include <string.h>
#include "Permutation.h"
using namespace std;

Permutation::Permutation() {
    /* nothing needed in the constructor */
}

void Permutation::permute(const string& str) {

    string stringnew = str;
    int j;
    int low = 0;
    int high = str.length();

    cout << stringnew << endl;

    for (j = 0; j <= high; j++) {
        string strtemp = stringnew[j];
        std::swap((strtemp + low), (strtemp + j));
        permute(str, low + 1, high);
        std::swap(str[j + low], str[j + j]);

    }
}

void Permutation::permute(const string& str, int low, int high) {
//  int j;
//  if (low == high) {
//      cout << str << endl;
//  } else {
//      for (j = low; j <= high; j++) {
//          std::swap(str[j + low], str[j + j]);
//          permute(str, low + 1, high);
//          std::swap(str[j + low], str[j + j]);
//      }
//  }
}
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    Prefer `std::swap` to making your own. – chris Dec 11 '12 at 23:41
  • 3
    You don't need to convert a string to an array. In Java, this is needed because Java strings are immutable. But C++ std::string is mutable, so you can work on it directly, swapping its elements etc. Basically just replace all mentions of `a` in your code to `str`. Oh, and make the string non-const. – Pavel Minaev Dec 11 '12 at 23:41
  • Can't you just use standard permutations with standard strings? – K-ballo Dec 11 '12 at 23:42
  • @PavelMinaev Can you give me more to go on...I'm slightly confused. If I make my a's into str's then what about my swap function? Don't I need to use like substr or something? I literally know nothing of C++ so this is decently hard haha – Austin Dec 12 '12 at 03:40
  • @Austin You don't need to use `substr` - `std::string` supports indexing with square brackets, same as array, and you can use the result on the left side of the assignment. – Pavel Minaev Dec 12 '12 at 21:20

3 Answers3

1

You must work through the class interface. You cannot get a writeable character array from a std::string.

What you can do is use the array subscript operator and access it as str[i]. You can also use iterators.

The reason for this is that prior to C++03, std::string was not required to be a character array. It could be discontinuous. At least one implementation used a std::deque style "array of pointers to arrays" backing store, which gave it fast insert, prepend and delete-from-the-middle abilities.

Also, from an Object Oriented programming design perspective, it is Not Nice to reach into an object's guts and rearrange them.

Just for fun because I wanted a break from work, some code that messes with a string using array subscripts:

#include <cctype>
#include <string>
#include <iostream>

void uc(std::string &s) 
{
    size_t i;
    const size_t len = s.length();
    for(i=0; i<len; ++i) {
        s[i] = toupper(s[i]);
    }   
}

void mix(std::string &s) 
{
    size_t i;
    const size_t len = s.length();
    for(i=1; i<len/2+1; ++i) {
        std::swap(s[i-1], s[len-i]);
    }   
}

int main()
{
    std::string s("Test String");
    uc(s);
    std::cout << s << std::endl;
    mix(s);
    std::cout << s << std::endl;
    return 0;
}
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Okay, so I changed my code around to this.. `void Permutation::permute(string& str, int low, int high) { int j; if (low == high) { cout << str << endl; } else { for (j = low; j <= high; j++) { std::swap(str[j + low], str[j + j]); permute(str, low + 1, high); std::swap(str[j + low], str[j + j]); }}}` However now my main is upset... `p.permute ("Permute"); p.permute ("--*--", 2, 4);` It says no matching function to call Permutation::permute(const char [6], int, int) – Austin Dec 12 '12 at 04:08
  • I was able to figure it out using this method. Thanks! `int low = 0; int high = str.length() - 1; // make sure the string is a permutation and not a partial mix. if (low == high) { cout << str << endl; } else { //Takes each initial letter, then permutes the remaining string. Then moves to next character. for (int i = low; i <= high; i++) { std::swap(str[low], str[i]); permute(str, low + 1, high); std::swap(str[low], str[i]); } } cout << endl << "--BREAK--" << endl << endl;` – Austin Dec 12 '12 at 05:58
0

Just take the c_str()-function

std::string str("I'm a text");
char *pStr = str.c_str();
Link
  • 1,307
  • 1
  • 11
  • 23
0

This is C++ not java as in thread you pointed. First of all

char[] x 

is valid declaration only for compile time known sizes of table.

The other thing is that std::string does not have .toCharArray method but it has .c_str() method you can use to get const char* from std::string.

HTH

Yester
  • 652
  • 6
  • 18