hello all

I needed to cut a string at a certain point and swap the position of the pieces so I did the following functions [it does not work well] :

void findcutswitch(string unlock_w, char ring[][26])
{
	for(int i=0; i<3; i++)
	{
		for(int j=0; j<26; j++)
		{
			if(ring[i][j]==unlock_w[i])
			{
				int y=i;
				int z=j;

				for(int u=0; u<i; u++)
					for(int p=0; p<j; p++)
						swapO(ring[y][z],ring[u][p]);
				y++;
				z++;
			}
		}
	}
}

void swapO(char &x, char &y)
{
	char temp;
	temp=x;
	x=y;
	y=temp;
}

your help is appreciated :)

To be honest, I don't really understand what you're doing with that code. But it looks like you're trying to swap the string 1 char at a time. But why not use build-in functions from like substr() en find() ?

Example:

#include <iostream>
#include <string>

std::string cut_and_swap(std::string in_str, char token)
{
    size_t pos = in_str.find_first_of(token);
    std::string out_str = in_str.substr(pos, in_str.size() - pos);
    out_str += in_str.substr(0,pos);
    return out_str;
}
int main()
{   
    std::cout << cut_and_swap("hai, this is a test - string", '-');
}

Do you have an example of input/output?
e.g. using 'substr' and 'erase' on a std::string

std::string str = "Hello, World";
    std::cout << str.append(str.substr(0,5)).erase(0,5);

"Hello, World!" cut at position 5 and appended to the end becomes ", WorldHello"

It would help knowing what your code is supposed to do before knowing what's wrong with it.

Edited 6 Years Ago by Bench: n/a

nick and bench thanks for replying well the idea is about cutting a string and switching parts

say you have this sentence "Thequickbrownfoxjumpedover"

the program is supposed to look for the letter "j" if it found "j" it turns the sentence to be"jumpedoverThequickbrownfox"

I hope I made it clearer

I do not quite understand your code sir "niek_e", I would appreciate it if you tell me what you did in each step

thanks :)

Edited 5 Years Ago by Nick Evan: n/a

The code nick used is pretty similar to the example i showed using an absolute position

given a string called str string str = "Thequickbrownfoxjumpsoverthelazydog"; (1) find the position of 'j' int where = str.find('j'); (2) store the substring of 0..where string snip = str.substr(0,where); (3) erase the substring of 0..where str.erase(0,where); (4) append the snipped string str += snip;

Edited 5 Years Ago by Nick Evan: n/a

I do not quite understand your code sir "nick", I would appreciate it if you tell me what you did in each step

Sir nick, haha :)

here you go:

#include <iostream>
#include <string>

std::string cut_and_swap(std::string in_str, char token)
{
    // find the first position of the token and store that position in pos
    size_t pos = in_str.find_first_of(token); 
    //create a new string "out_str" which contains the the last part of the input string.
    std::string out_str = in_str.substr(pos, in_str.size() - pos);
    //add the first part of the string to the last
    out_str += in_str.substr(0,pos);
    return out_str;
}
int main()
{   
    std::cout << cut_and_swap("hai, this is a test - string", '-');
}

Read about substr() here

[edit]
Whoops, cross post with Bench. Glad to see you're back!

Edited 5 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.