如何从字符串"Fold a word"。前任。 "STACK"变得"SKTCA".C++
How to "Fold a word" from a string. EX. "STACK" becomes "SKTCA". C++
我正在尝试找出如何从字符串中折叠单词。例如,折叠后的"代码"将成为" CEOD"。基本上是从第一个字符开始,然后获得最后一个字符,然后是第二个字符。我知道第一步是从循环开始,但是我不知道如何获得最后一个角色。任何帮助都会很棒。这是我的代码。
#include <iostream>
using namespace std;
int main () {
string fold;
cout << "Enter a word: ";
cin >> fold;
string temp;
string backwards;
string wrap;
for (unsigned int i = 0; i < fold.length(); i++){
temp = temp + fold[i];
}
backwards= string(temp.rbegin(),temp.rend());
for(unsigned int i = 0; i < temp.length(); i++) {
wrap = fold.replace(backwards[i]);
}
cout << wrap;
}
谢谢
@supreme,有许多方法可以完成您的任务,我将发布其中之一。但是正如@John指出的那样,您必须尝试自己的工作来完成它,因为真正的编程就是要练习很多。使用此解决方案作为一种可能性的参考并找到许多其他可能性。
int main()
{
string in;
cout <<"enter: "; cin >> in;
string fold;
for (int i=0, j=in.length()-1; i<in.length()/2; i++, j--)
{
fold += in[i];
fold += in[j];
}
if( in.length()%2 != 0) // if string lenght is odd, pick the middle
fold += in[in.length()/2];
cout << endl << fold ;
return 0;
}
祝你好运!
有两种方法解决了这种形式的问题,数学上精确的方法是创建一个以正确顺序返回数字的生成器函数。
一个更容易的计划是修改字符串以解决问题。
数学解决方案
我们想要一个函数,该函数返回字符串中的索引添加。我们有2个序列 - 增加和减少,它们交错。
序列1:
0, 1 , 2, 3.
序列2
len-1, len-2, len-3, len-4.
鉴于它们是交错的,我们甚至希望值来自序列1,而奇数值来自序列2。
。因此,我们的解决方案将用于给定的新索引,选择要使用的序列,然后从该序列返回下一个值。
int generator( int idx, int len )
{
ASSERT( idx < len );
if( idx %2 == 0 ) { // even - first sequence
return idx/2;
} else {
return (len- (1 + idx/2);
}
}
然后可以从函数折叠...
来调用。std::string fold(const char * src)
{
std::string result;
std::string source(src);
for (size_t i = 0; i < source.length(); i++) {
result += source.at(generator(i, source.length()));
}
return result;
}
pratical解决方案
尽管效率较低,但这可以更容易考虑。我们正在采用字符串的第一个或最后一个字符。我们将使用字符串操纵来获得正确的结果。
std::string fold2(const char * src)
{
std::string source = src;
enum whereToTake { fromStart, fromEnd };
std::string result;
enum whereToTake next = fromStart;
while (source.length() > 0) {
if (next == fromStart) {
result += source.at(0);
source = source.substr(1);
next = fromEnd;
}
else {
result += source.at(source.length() - 1); // last char
source = source.substr(0, source.length() - 1); // eat last char
next = fromStart;
}
}
return result;
}
您可以利用反向迭代器的概念来根据usman riaz答案中提出的解决方案编写通用算法。
从原始字符串的两个末端组成字符串挑选字符。当您到达中心时,如果字符的数量很奇。
这是可能的实现:
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <iterator>
template <class ForwardIt, class OutputIt>
OutputIt fold(ForwardIt source, ForwardIt end, OutputIt output)
{
auto reverse_source = std::reverse_iterator<ForwardIt>(end);
auto reverse_source_end = std::reverse_iterator<ForwardIt>(source);
auto source_end = std::next(source, std::distance(source, end) / 2);
while ( source != source_end )
{
*output++ = *source++;
*output++ = *reverse_source++;
}
if ( source != reverse_source.base() )
{
*output++ = *source;
}
return output;
}
int main() {
std::vector<std::pair<std::string, std::string>> tests {
{"", ""}, {"a", "a"}, {"stack", "sktca"}, {"steack", "sktcea"}
};
for ( auto const &test : tests )
{
std::string result;
fold(
std::begin(test.first), std::end(test.first),
std::back_inserter(result)
);
std::cout << (result == test.second ? " OK " : "FAILED: ")
<< '"' << test.first << "" --> "" << result << ""n";
}
}
相关文章:
- std::stack 是连续的吗?
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 堆叠协程 + gdb = "previous frame inner to this frame (corrupt stack)?"
- 生成质数的程序,错误:"Stack overflow"
- 如何在 x64 上"stack oveflow"例外
- 为什么此代码中显示"*** stack smashing detected ***: <unknown> terminated Aborted (core dumped) "错误?
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- 无法在基于 DFS 的任务排序程序中填充"stack"
- std::stack的奇怪行为,pop()返回相同的值
- 使用双LL在C++中实现Stack失败,出现异常"EXC_BAD_ACCESS(代码=2,地址=0x7fff5
- 如何获得"基于堆栈"的std::stack?(具有固定的最大尺寸)
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?
- 如何修复初始化后'stack around variable was corrupted.'变量未更改为分配的值
- Ctypes: OSError: exception: stack overflow
- FLEX/BISON在不同的文件夹中生成location.hh,position.hh,stack.hh
- C++ stack with unique_ptr
- 是否有与C++"-Wl,--stack,[a number, e.g. 4194304 for 4Mb of stack]"的 CUDA 等效性?
- 在Macbook Pro上实现clang -fno-stack-protector编译代码时出现问题
- 如何从字符串"Fold a word"。前任。 "STACK"变得"SKTCA".C++