Reputation:
I have a character array, and I want to move it certain positions starting from a fixed position, and putting spaces for the amount of positions moved.
For example if I have the array:
(read A var := 5)
And if I want to move it one place from the position one, I want to get:
( read A var := 5)
But instead I get:
((ead A var := 5)
Below you can find my source code:
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;
char output[100] = "(read A var := 5)";
void move(int spos, int places)
{
int i = places, j = spos;
for (int k = 0; k < places; k++) {
output[j+i] = output[j];
i++;
j++;
}
}
int main(void)
{
for (i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
move(0, 1);
output[18] = '\0';
for (i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
return 0;
}
Upvotes: 1
Views: 6304
Reputation: 392911
I've actually written this to the now-deleted question which was quite similar:
I hope it helps: Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
using It = std::string::iterator;
using tokref = boost::iterator_range<It>;
using swappables = std::pair<tokref, tokref>;
namespace qi = boost::spirit::qi;
int main()
{
std::string input = "read A var := 5";
It first(input.begin()), last(input.end());
using boost::phoenix::construct;
using boost::phoenix::push_back;
using namespace qi;
qi::rule<It, tokref()> token_;
qi::rule<It, swappables(), qi::space_type> swap_;
token_ = qi::raw[+qi::graph];
swap_ = (token_ >> token_) [ _val = construct<swappables>(_1, _2) ];
std::vector<swappables> to_swap;
std::cout << "Before: " << input << "\n";
if (qi::phrase_parse(first, last,
*(
(&as_string [token_] [ _pass = ("var" == _1) ] >> swap_)
| omit[ token_ ]
), space, to_swap))
{
// swap all swappables!
for (auto& pair : to_swap)
{
std::cout << "Swap '" << pair.first << "' and '" << pair.second << "'\n";
auto& a = pair.first;
auto& b = pair.second;
input.replace(a.begin(), b.end(),
std::string(b.begin(), b.end()) +
std::string(a.end(), b.begin()) +
std::string(a.begin(), a.end()));
}
}
std::cout << "After: " << input << "\n";
}
Output:
read A var := 5
Swap 'var' and ':='
read A := var 5
Upvotes: 0
Reputation: 26
Here is my version of move function, try it:
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;
char output[100] = "(read A var := 5)";
/* my version of move ;) */
void move2(int spos, int places)
{
for(int x = strlen(output); x >=spos; x--)
{
output[x+places] = output[x]; /* move character from position x to position x+places */
}
for(int x = 0; x < places; x++)
output[x+spos] = ' ' ; /* now we are adding white spaces to the begining */
}
int main(void)
{
int i = 0;
for(i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
move2(1, 1);
for (i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
return 0;
}
Upvotes: 1
Reputation:
Try this :
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;
char output[100] = "(read A var := 5)";
void move(int spos, int shift_Len)
{
int j = spos;
int i = shift_Len;
for (int k = strlen(output); k >j; k--) {
output[k+i]= output[k-1];
}
for (int len=0 ; len <=i ; len++ ) {
output[j+len]=' '; //fill shift position with space char
}
}
int main(void)
{
int i;
for (i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
move(5,10); //first argument is shift position and second argumnet is number of shift
output[strlen(output)] = '\0';
for (i = 0; i < strlen(output); i++)
cout << output[i];
cout << endl;
return 0;
}
Upvotes: 0