我有一个字符数组,我想从固定位置开始移动某些位置,并为移动的位置数量添加空格。
例如,如果我有数组:
(read A var := 5)
如果我想从第一个位置移动一个位置,我想得到:
( read A var := 5)
但我得到了:
((ead A var := 5)
您可以在下面找到我的源代码:
#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;
}
答案 0 :(得分:1)
这是我的移动功能版本,请尝试:
#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;
}
答案 1 :(得分:0)
试试这个:
#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;
}
答案 2 :(得分:0)
我实际上已将此写入现已删除的问题,该问题非常相似:
我希望它有所帮助: 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";
}
输出:
read A var := 5
Swap 'var' and ':='
read A := var 5