Hi,

I am having trouble comparing characters from two strings.

This is what I have

void encrypt(string plaintext, string vigenere_table[length][length], string keyword, string& ciphertext)
{
	int row, col;
         for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
		      if(vigenere_table[x][1] == plaintext[i])  //wont let me use ==
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
		      if(vigenere_table[0][y] == keyword[i])   //wont let me use ==
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];
	 }
}

Thanks in advance.

You had declared it as string variables. In C, strings are nothing but NULL terminated character arrays.

You can overload the == operator(since you have already used the string class) OR you could write the 2D string as:

char [len][len]; // len in const
and the 1D string as
char[]

Cant I do anything about it, if I want to keep it string.
As I have used the variables plaintext and keyword in my main function as strings, and now if I change them into char array I am getting output with some weird symbols.

I didnt get you when you said that "You can overload the == operator(since you have already used the string class)" ??

Yes you can.

>You can overload the == operator(since you have already used the string class)

By that i meant, since you have already coded in C++, you can as well overload == to give it a different meaning. ie in your case, since you need to compare the two characters.

OR you can use a 1D string array. and for comparing, you could do something like this:

string s[10] = {"hello", "world"};

	if (s[0][0] == 'h')
		// do something here
void encrypt(string plaintext, string vigenere_table[length], string keyword, string& ciphertext)
{
	int row, col;
         for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
		      if(vigenere_table[x][1] == plaintext[i])  // Now you can compare
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
		      if(vigenere_table[0][y] == keyword[i])   // Now you can compare 
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];
	 }
}

Hope you got my point. So basically thats what you needed. for comparing. Hope this helps.

If I make those changes in my code, then how should I write my function call?

Right now, thats what I have:

encrypt(plaintext, vigenere_table, keyword, ciphertext);

Really appreciate your time and help.

Yeah that would be just fine.

eg:

void do_nothing(string s[])
{
	if (s[0][0] == 'h')
		cout << "correct"; 	
}

int main()
{	
	string s[10] = {"hello", "world"};	

	do_nothing(s);
	
	return 0;
}

i guess this is similar to your function. So, the function call that you had posted is fine.

Actually in my main I have declared 2D as:

string vigenere_table[length][length];

and I have another function for intiallizing it.

main()
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz ";
    string vigenere_table[length][length];

    init_vigenere_table(alphabet, vigenere_table);
}

void init_vigenere_table(string& alphabet, string vigenere_table[length][length])
{
     for(int i = 0; i<length; i++)
     {
             vigenere_table[0][i] = alphabet[i];     
     }
     for(int i=1; i<length; i++)
     {
          shift(alphabet, i, vigenere_table);     
     }
     
}

Here I am generating a table of alphabets.

So I dont know what should I change in order to make it like your example of string s[].

And I am getting an error in my function call:

encrypt(plaintext, vigenere_table, keyword, ciphertext);

error C2664: 'encrypt' : cannot convert parameter 2 from 'std::string [27][27]' to 'std::string []'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Basically, the problem is, you have declared a 2D string array in those functions too. So, wherever you are using the table you mentioned, use a 1D string.

And i did not quite follow what you were doing in init_vigenere_table() function. Could you elaborate a little bit?

This is what I am initializing the 2D array with.
I have a string of alphabets and I keep shifting left in order to get the following table. So every next line in the table is shifted left one character.


a b c d e f g h i j k l m n o p q r s t u v w x y z
b c d e f g h i j k l m n o p q r s t u v w x y z a
c d e f g h i j k l m n o p q r s t u v w x y z a b
d e f g h i j k l m n o p q r s t u v w x y z a b c
e f g h i j k l m n o p q r s t u v w x y z a b c d
f g h i j k l m n o p q r s t u v w x y z a b c d e
g h i j k l m n o p q r s t u v w x y z a b c d e f
h i j k l m n o p q r s t u v w x y z a b c d e f g
i j k l m n o p q r s t u v w x y z a b c d e f g h
j k l m n o p q r s t u v w x y z a b c d e f g h i
k l m n o p q r s t u v w x y z a b c d e f g h i j
l m n o p q r s t u v w x y z a b c d e f g h i j k
m n o p q r s t u v w x y z a b c d e f g h i j k l
n o p q r s t u v w x y z a b c d e f g h i j k l m
o p q r s t u v w x y z a b c d e f g h i j k l m n
p q r s t u v w x y z a b c d e f g h i j k l m n o
q r s t u v w x y z a b c d e f g h i j k l m n o p
r s t u v w x y z a b c d e f g h i j k l m n o p q
s t u v w x y z a b c d e f g h i j k l m n o p q r
t u v w x y z a b c d e f g h i j k l m n o p q r s
u v w x y z a b c d e f g h i j k l m n o p q r s t
v w x y z a b c d e f g h i j k l m n o p q r s t u
w x y z a b c d e f g h i j k l m n o p q r s t u v
x y z a b c d e f g h i j k l m n o p q r s t u v w
y z a b c d e f g h i j k l m n o p q r s t u v w x
z a b c d e f g h i j k l m n o p q r s t u v w x y
a b c d e f g h i j k l m n o p q r s t u v w x y z

Now I have changed 2D string to 2D character array, so now its like:

main()
{
     char vigenere_table[length][length];
     encrypt(plaintext, vigenere_table[length], keyword, ciphertext);
}
void encrypt(string plaintext, char vigenere_table[length], string keyword, string& ciphertext)
{
	string temp = keyword;
	int row, col;
     while(keyword.length() < plaintext.length())
     {
            keyword += temp;
     }
	 keyword = keyword.substr(0, 20);
     for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
			 if(vigenere_table[x][1] == plaintext[i])
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
			 if(vigenere_table[0][y] == keyword[i])
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];  //error in col
                                                            //error C2109: subscript 
                                                            //requires array or 
                                                            //pointer type
	 }
}

I am sorry if it sounds confusing!

I have made a few changes, its working, but not giving me desired output. I think there is a problem with the comparison.
Here is my update code:

main()
{
    encrypt(plaintext, vigenere_table, keyword, ciphertext);
}
void encrypt(string plaintext, char vigenere_table[length][length], string keyword, string& ciphertext)
{
	string temp = keyword;
	int row, col;
     while(keyword.length() < plaintext.length())
     {
            keyword += temp;
     }
	 keyword = keyword.substr(0, 20);
     for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
			 if(vigenere_table[x][1] == plaintext[i])
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
			 if(vigenere_table[0][y] == keyword[i])
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];
	 }
}

Hey, I GOT IT!!

THANKS A MILLION for your help.

There was an error in my coding!

Oops i didn't your posts in the next page! and i posted again by mistake! Now i don't know how to delete this :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.