Angersmash
Angersmash

Reputation: 257

Basic obfuscation program

#include<iostream>
#include<conio.h>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
using namespace std;

int main() {
    int k=0;
    string s;

    cout<<"string "; 

    getline(cin,s);             //taking in a string from the user

    float n=s.size();          //storing size of string

    int f=floor((sqrt(n)));   //floor of square root of input string

    int c=ceil((sqrt(n)));    //ceiling 

    int m=f*c;               //storing product of f and c

     vector< vector<string> > vec(n<=m?f:++f, vector<string>(c)); //makes a 2d vector 
                                                                  //depending on user's 
                                                                  //string length


    for(int i=0;n<=m?i<f:i<++f;i++)        //looping acc to user's input and assigning   
    {
        for(int j=0;j<c;j++)           //string to a matrix   
        {
            if(k<s.size())
            {
                vec[i][j]=s[k];
                k++;
            }
        }
    }



    for(int j=0;j<c;j++)        //printing the vector
        {

    {
        for(int i=0;n<=m?i<f:i<++f;i++)

            cout<<vec[i][j];

    }cout<<" ";
        }

getch();         

}

It's not working for n>m as for a string of length 8 characters it makes a vector of 2*3 thus failing to enclose the whole string in the matrix and which is why I am using ternary so as to make a vector of bigger size when it encounters cases like these. .So what am I doing wrong?

I'll just write the whole question.

One classic method for composing secret messages is called a square code.  The spaces are removed from the english text and the characters are written into a square (or rectangle). The width and height of the rectangle have the constraint,

    floor(sqrt(word)) <= width, height <= ceil(sqrt(word))

    The coded message is obtained by reading down the columns going left to right. For example, the message above is coded as:

    imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau


    Sample Input:

    chillout

    Sample Output:

    clu hlt io

Upvotes: 2

Views: 523

Answers (1)

Jamal
Jamal

Reputation: 771

This won't fix your entire problem, but I still feel it is important. You seem to misunderstand how the ternary works. Let's observe one of its uses here:

for (int i = 0; n <= m ? i < f : i < ++f; i++) {}
//              ^^^^^^^^^^^^^^^^^^^^^^^^  <--- not the intended outcome

This will not work because the returned side of the ternary does not "stick" itself in-place. In other words, neither i < f nor i < ++f will be put directly into the for-loop. Instead, it'll give you a value.

To see what it's really doing, you'll first need to understand that the ternary is just another way to do an if-else. The ternary above, put into if-else form, looks like this:

if (n <= m)
    i < f;   // left side of the ":"
else
    i < ++f; // right side of the ":"

Let's break it down further:

i < f

This is doing a less-than comparison of i and f. So, depending on the individual values, you'll receive either a 0 (false) or a 1 (true).

So, in your for-loop, this will occur:

for (int i = 0; 1; i++) {}
//              ^  <--- if comparison returns true

for (int i = 0; 0; i++) {}
//              ^  <--- if comparison returns false

So, for your example, you'll need to find the value of f before the loop. You can use a ternary for that part, but only if you understand it. Otherwise, use another method to find f (the intended numerical value). Once you find it, then you can put i < f into the for-loop.

Upvotes: 2

Related Questions