How to make this cypher more flexible and efficient [on hold]












-1












$begingroup$


so I have been asked to make a cypher which can reverse the order of characters.
Say "ajay"
output "yaja"



so far i have used a very basic botched up code



 #include <iostream>

using namespace std;


int main()
{
char a;
char b;
char c;
char d;

cout<< "enter wordn";
cin>>a;
cin>>b;
cin>>c;
cin >>d;


cout<<d<<c<<b<<a;



return 0;
}


the code cannot work with lesser characters than 4 and obviously, it doesn't work on characters greater than 4.



plus it just looks way too, crappy and direct.
I have started coding from today itself so apologies in advance if the question doesn't match the standards of the community.










share|improve this question







New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 4




    $begingroup$
    You are asking a bit too much for this site to help.
    $endgroup$
    – ratchet freak
    14 hours ago






  • 3




    $begingroup$
    I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
    $endgroup$
    – Felix
    13 hours ago


















-1












$begingroup$


so I have been asked to make a cypher which can reverse the order of characters.
Say "ajay"
output "yaja"



so far i have used a very basic botched up code



 #include <iostream>

using namespace std;


int main()
{
char a;
char b;
char c;
char d;

cout<< "enter wordn";
cin>>a;
cin>>b;
cin>>c;
cin >>d;


cout<<d<<c<<b<<a;



return 0;
}


the code cannot work with lesser characters than 4 and obviously, it doesn't work on characters greater than 4.



plus it just looks way too, crappy and direct.
I have started coding from today itself so apologies in advance if the question doesn't match the standards of the community.










share|improve this question







New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 4




    $begingroup$
    You are asking a bit too much for this site to help.
    $endgroup$
    – ratchet freak
    14 hours ago






  • 3




    $begingroup$
    I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
    $endgroup$
    – Felix
    13 hours ago
















-1












-1








-1





$begingroup$


so I have been asked to make a cypher which can reverse the order of characters.
Say "ajay"
output "yaja"



so far i have used a very basic botched up code



 #include <iostream>

using namespace std;


int main()
{
char a;
char b;
char c;
char d;

cout<< "enter wordn";
cin>>a;
cin>>b;
cin>>c;
cin >>d;


cout<<d<<c<<b<<a;



return 0;
}


the code cannot work with lesser characters than 4 and obviously, it doesn't work on characters greater than 4.



plus it just looks way too, crappy and direct.
I have started coding from today itself so apologies in advance if the question doesn't match the standards of the community.










share|improve this question







New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




so I have been asked to make a cypher which can reverse the order of characters.
Say "ajay"
output "yaja"



so far i have used a very basic botched up code



 #include <iostream>

using namespace std;


int main()
{
char a;
char b;
char c;
char d;

cout<< "enter wordn";
cin>>a;
cin>>b;
cin>>c;
cin >>d;


cout<<d<<c<<b<<a;



return 0;
}


the code cannot work with lesser characters than 4 and obviously, it doesn't work on characters greater than 4.



plus it just looks way too, crappy and direct.
I have started coding from today itself so apologies in advance if the question doesn't match the standards of the community.







c++






share|improve this question







New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 14 hours ago









Ayush UpadhyayAyush Upadhyay

1




1




New contributor




Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ayush Upadhyay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250 9 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – ratchet freak, Incomputable, Sᴀᴍ Onᴇᴌᴀ, 200_success, Hosch250

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 4




    $begingroup$
    You are asking a bit too much for this site to help.
    $endgroup$
    – ratchet freak
    14 hours ago






  • 3




    $begingroup$
    I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
    $endgroup$
    – Felix
    13 hours ago
















  • 4




    $begingroup$
    You are asking a bit too much for this site to help.
    $endgroup$
    – ratchet freak
    14 hours ago






  • 3




    $begingroup$
    I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
    $endgroup$
    – Felix
    13 hours ago










4




4




$begingroup$
You are asking a bit too much for this site to help.
$endgroup$
– ratchet freak
14 hours ago




$begingroup$
You are asking a bit too much for this site to help.
$endgroup$
– ratchet freak
14 hours ago




3




3




$begingroup$
I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
$endgroup$
– Felix
13 hours ago






$begingroup$
I would encourage you to keep reading / watching videos on basic programming concepts and data structures, and solve problems like this. You'll get so much information if you're just starting out. Specifically for this task I might direct you to the concept of arrays and lists. The question is, as @ratchetfreak pointed out, a bit off topic. But it's good that you seek guidance! Come back with the implementation you come up with for the inverter and people might respond much better.
$endgroup$
– Felix
13 hours ago












1 Answer
1






active

oldest

votes


















0












$begingroup$

When you're faced with a problem like this, you want to analyze what's needed to solve the problem then translate that to code.



What's the best data structure to hold the solution?



Since that problem calls for a collection of characters that need to be manipulated, the best structure for that is string.



What process is best to accomplish the goal?



Firstly, instead of processing everything in main, let's start with a function to do the heavy lifting. It needs to take a string as input and return a string.



To reverse the string, start by analyzing two strings. One the reverse of the other. It's easy to see that successive swaps(first and last, first + 1 and last - 1, first + 2 and last - 2, etc.), would be very efficient in terms of time and memory.



At this point, one can see that dealing with hard limits a for loop would be the simplest loop to accomplish this.



Now what would some code look like?



string ReverseString(string input)
{
size_t mid = input.size() / 2;
for (size_t start = 0, end = input.size() - 1; start < mid; ++start, --end)
{
char tempChar = input[start];
input[start] = input[end];
input[end] = tempChar;
}
return input;
}


For this I've assumed that you want a literal solution. The c++ standard library includes a function(reverse in the <algorithm> library) that will accomplish the same thing.






share|improve this answer









$endgroup$




















    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    When you're faced with a problem like this, you want to analyze what's needed to solve the problem then translate that to code.



    What's the best data structure to hold the solution?



    Since that problem calls for a collection of characters that need to be manipulated, the best structure for that is string.



    What process is best to accomplish the goal?



    Firstly, instead of processing everything in main, let's start with a function to do the heavy lifting. It needs to take a string as input and return a string.



    To reverse the string, start by analyzing two strings. One the reverse of the other. It's easy to see that successive swaps(first and last, first + 1 and last - 1, first + 2 and last - 2, etc.), would be very efficient in terms of time and memory.



    At this point, one can see that dealing with hard limits a for loop would be the simplest loop to accomplish this.



    Now what would some code look like?



    string ReverseString(string input)
    {
    size_t mid = input.size() / 2;
    for (size_t start = 0, end = input.size() - 1; start < mid; ++start, --end)
    {
    char tempChar = input[start];
    input[start] = input[end];
    input[end] = tempChar;
    }
    return input;
    }


    For this I've assumed that you want a literal solution. The c++ standard library includes a function(reverse in the <algorithm> library) that will accomplish the same thing.






    share|improve this answer









    $endgroup$


















      0












      $begingroup$

      When you're faced with a problem like this, you want to analyze what's needed to solve the problem then translate that to code.



      What's the best data structure to hold the solution?



      Since that problem calls for a collection of characters that need to be manipulated, the best structure for that is string.



      What process is best to accomplish the goal?



      Firstly, instead of processing everything in main, let's start with a function to do the heavy lifting. It needs to take a string as input and return a string.



      To reverse the string, start by analyzing two strings. One the reverse of the other. It's easy to see that successive swaps(first and last, first + 1 and last - 1, first + 2 and last - 2, etc.), would be very efficient in terms of time and memory.



      At this point, one can see that dealing with hard limits a for loop would be the simplest loop to accomplish this.



      Now what would some code look like?



      string ReverseString(string input)
      {
      size_t mid = input.size() / 2;
      for (size_t start = 0, end = input.size() - 1; start < mid; ++start, --end)
      {
      char tempChar = input[start];
      input[start] = input[end];
      input[end] = tempChar;
      }
      return input;
      }


      For this I've assumed that you want a literal solution. The c++ standard library includes a function(reverse in the <algorithm> library) that will accomplish the same thing.






      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        When you're faced with a problem like this, you want to analyze what's needed to solve the problem then translate that to code.



        What's the best data structure to hold the solution?



        Since that problem calls for a collection of characters that need to be manipulated, the best structure for that is string.



        What process is best to accomplish the goal?



        Firstly, instead of processing everything in main, let's start with a function to do the heavy lifting. It needs to take a string as input and return a string.



        To reverse the string, start by analyzing two strings. One the reverse of the other. It's easy to see that successive swaps(first and last, first + 1 and last - 1, first + 2 and last - 2, etc.), would be very efficient in terms of time and memory.



        At this point, one can see that dealing with hard limits a for loop would be the simplest loop to accomplish this.



        Now what would some code look like?



        string ReverseString(string input)
        {
        size_t mid = input.size() / 2;
        for (size_t start = 0, end = input.size() - 1; start < mid; ++start, --end)
        {
        char tempChar = input[start];
        input[start] = input[end];
        input[end] = tempChar;
        }
        return input;
        }


        For this I've assumed that you want a literal solution. The c++ standard library includes a function(reverse in the <algorithm> library) that will accomplish the same thing.






        share|improve this answer









        $endgroup$



        When you're faced with a problem like this, you want to analyze what's needed to solve the problem then translate that to code.



        What's the best data structure to hold the solution?



        Since that problem calls for a collection of characters that need to be manipulated, the best structure for that is string.



        What process is best to accomplish the goal?



        Firstly, instead of processing everything in main, let's start with a function to do the heavy lifting. It needs to take a string as input and return a string.



        To reverse the string, start by analyzing two strings. One the reverse of the other. It's easy to see that successive swaps(first and last, first + 1 and last - 1, first + 2 and last - 2, etc.), would be very efficient in terms of time and memory.



        At this point, one can see that dealing with hard limits a for loop would be the simplest loop to accomplish this.



        Now what would some code look like?



        string ReverseString(string input)
        {
        size_t mid = input.size() / 2;
        for (size_t start = 0, end = input.size() - 1; start < mid; ++start, --end)
        {
        char tempChar = input[start];
        input[start] = input[end];
        input[end] = tempChar;
        }
        return input;
        }


        For this I've assumed that you want a literal solution. The c++ standard library includes a function(reverse in the <algorithm> library) that will accomplish the same thing.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        tinstaafltinstaafl

        6,5311827




        6,5311827















            Popular posts from this blog

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”