反转字符串变量,或反向打印char数组

时间:2018-04-06 01:22:13

标签: c arrays char printf scanf

所以我一直在寻找并试图找到一个简单的解决这个问题的方法,这个问题不涉及在c ++中非常常见的“cin”,“cout”的解决方案。我想要完成的是从键盘扫描一个条目,然后以相反的顺序打印它。我认为我的语法有问题,但我不知道问题是什么。

使用“abc”作为输入5次后得到的输出是:

//Start of input
Enter a string:
abc
abc
abc
abc
abc
╠
cba
cba
cba
cba
cb
Enter a string:
//End of output

这是我的代码。

#include "stdafx.h"
#include <stdio.h>
#include <string>
#define SIZE 20

int main()
{
    char a[SIZE];
    int i, j, k = 0, pass;
    char hold = ' ';

    for (i = 1; i != 0; i++)
    {
        printf("Enter a string: \n");

        for (i = 0; i < SIZE; i++)
        {
            scanf_s("%c", &a[i]);
            k++;
        }

        for (j = k; j > 0; j--)
        {
            printf("%c", a[j]);
        }

        printf("\n");
    }
    return 0;
}

4 个答案:

答案 0 :(得分:1)

 for (j = k; j > 0; j--)
    {
        printf("%c", a[j]);
    }

有问题 为什么不迭代[0]?

看,这是你的输入

 for (i = 0; i < SIZE; i++)
    {
        scanf_s("%c", &a[i]);
        k++;
    }

你在[0]中保留了一个角色,所以当然你应该打印它,如:

for (j = k; j >= 0; j--)
    {
        printf("%c", a[j]);
    }

答案 1 :(得分:0)

没有评论为什么你实际上正在使用C ++编译器。

对于你的问题,你应该倒数到0来移动整个a

for (j = k; j >= 0; j--) {
    printf("%c", a[j]);
}

答案 2 :(得分:0)

我只能给你逻辑。但是数组是声明和执行此操作的简单方法。或者当你进行循环时,将其声明为反向。希望它能帮助您思考可能的答案。

engine: /usr/include/boost/intrusive/bstree.hpp:1331: boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::iterator boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::insert_unique_commit(boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::reference, const insert_commit_data&) [with ValueTraits = boost::intrusive::bhtraits<boost::container::container_detail::tree_node<std::pair<const int, event>, boost::interprocess::offset_ptr<void>, (boost::container::tree_type_enum)0, true>, boost::intrusive::rbtree_node_traits<boost::interprocess::offset_ptr<void>, true>, (boost::intrusive::link_mode_type)0, boost::intrusive::dft_tag, 3>; VoidOrKeyOfValue = void; VoidOrKeyComp = boost::container::value_to_node_compare<boost::container::container_detail::tree_node<std::pair<const int, event>, boost::interprocess::offset_ptr<void>, (boost::container::tree_type_enum)0, true>, boost::intrusive::tree_value_compare<boost::interprocess::offset_ptr<std::pair<const int, event>, long int, long unsigned int, 0>, std::less<int>, boost::container::container_detail::select1st<int>, false> >; SizeType = long unsigned int; bool ConstantTimeSize = true; boost::intrusive::algo_types AlgoType = (boost::intrusive::algo_types)5; HeaderHolder = void; boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::iterator = boost::intrusive::tree_iterator<boost::intrusive::bhtraits<boost::container::container_detail::tree_node<std::pair<const int, event>, boost::interprocess::offset_ptr<void>, (boost::container::tree_type_enum)0, true>, boost::intrusive::rbtree_node_traits<boost::interprocess::offset_ptr<void>, true>, (boost::intrusive::link_mode_type)0, boost::intrusive::dft_tag, 3>, false>; boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::reference = boost::container::container_detail::tree_node<std::pair<const int, event>, boost::interprocess::offset_ptr<void>, (boost::container::tree_type_enum)0, true>&; boost::intrusive::bstree_impl<ValueTraits, VoidOrKeyOfValue, VoidOrKeyComp, SizeType, ConstantTimeSize, AlgoType, HeaderHolder>::insert_commit_data = boost::intrusive::insert_commit_data_t<boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> >, long int, long unsigned int, 0> >]: Assertion `( p == this->end() || !this->comp()(*p, value) )' failed.
Aborted (core dumped)

答案 3 :(得分:0)

在C ++中,它将以这种方式完成:

RewriteCond %{HTTP_HOST} ^(example1|example2)\.(com|net|co)$ [NC]

如果您想要一个不涉及C ++的答案,请将标签从C ++更改为C.

相关问题