Hi
I am writing a toy encryption method objecy oriented program in which user enters a statement, and each character in the statement is replaced by another character which are stored in a 2D array.

I have created a static array in which each alphabet has a value in 2D array something like this and the program also.... I am having problem in the code with red area...
Actually i am going worng in compairng..Could anyone please help me..

#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
const int row =26;
const int col= 2;
const char tarray[row][col]=
{
{'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', '}'}
};
class toymethod
{
char string[30];
public:
void encrypt();
void decrypt();
};


void toymethod::encrypt()
{
int k;
cout<<"Enter the string \n";
cin.getline(string,20);do
{
if(strcmp(string,tarray)==0)
cout<<tarray[j];
else
cout<<"Character not found \n";
}while(i<26);
}void toymethod::decrypt()
{
}


void main()
{


toymethod t1;
t1.encrypt();


}

Please help me...THanks in advance...Bye

Dani AI

Generated

Short summary and fixes for : the core problem is trying to use strcmp against your 2D table. strcmp expects two null-terminated C strings. Your tarray is a 26x2 table of pairs, not a single C string, so the comparison is invalid. was right that you need to compare characters (one input char against one table entry) and has a working approach that uses indexing. Besides that, avoid tiny fixed char buffers, prefer std::string and std::getline, and never rely on nonstandard headers like conio.h.

A cleaner, safer pattern is to build a fast lookup (map) for encryption and an inverse map for decryption. This keeps the logic simple, is easy to extend (lowercase, punctuation), and avoids nested scans for each character. Example encrypt helper (fill the map elsewhere):

#include <string>
#include <unordered_map>
#include <cctype>

std::string encrypt(const std::string& in, const std::unordered_map<char,char>& map) {
std::string out;
out.reserve(in.size());
for (unsigned char c : in) {
char key = std::toupper(c); // normalize to table keys
auto it = map.find(key);
out.push_back(it != map.end() ? it->second : c); // keep char if no mapping
}
return out;
}

Notes and cautions: use unsigned char when calling std::toupper to avoid UB, decide how to treat lowercase and non-alpha characters, and remember a substitution table is not secure crypto—it is fine for learning or obfuscation but not for real security.

Recommended Answers

All 2 Replies

strcmp takes two char*'s as input, but tarray is a 2 charactor wide array. It seems like you want to compare each char of 'string' with each row in tarray, as in:

if (string == tarray[j][0])

i.e., the ith char of string with the jth row of tarray, and column 0 which is the column with upper case alphas.

This means you need TWO loops, one that iterates over every char in string that was entered, and another that iterates over the 26 rows of tarray, looking for that char.

Another issue here is that you only translate UPPERCASE chars. Does that mean that you don't want to convert lower-case letters?

after playing with your code

#include<iostream>
using namespace std;
  
const int n_row =26;
const int n_col= 2;
const char tarray[n_row][n_col]= {
    {'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', '}'} };

class toyclass {  // didn't like the name
  char m_string[30];
public:
  void encrypt();
  void decrypt();
private:
  char lookup( char encoded );
};

void toyclass::encrypt() {
  char inp[30];
  int idx;
  cout<<"Enter the string \n";
  cin.getline(inp,30);
  for( idx = 0; inp[idx] != 0; idx++ ) {
     int cidx = inp[idx] - 'A';
     if ( cidx >= 0 && cidx < 26 ) 
       m_string[idx] = tarray[cidx][1];
     else 
       cout<<"Character '" << inp[idx] << "' not found \n";
  }
  m_string[idx] = '\0';

  cout << "encrypted '" << m_string << "'\n";
}

char toyclass::lookup( char encoded ) {
  char decoded = '*';
  int idx = 0;
  while ( idx < n_row ) {
     if ( tarray[idx][1] == encoded ) {
        decoded = tarray[idx][0];
        break;
     }
     idx++;
  }
  return decoded;
}

void toyclass::decrypt(){
  int idx;
  char outp[30];
  for( idx = 0; m_string[idx] != 0; idx++ )
     outp[idx] = lookup(m_string[idx]);
  outp[idx] = '\0';
  cout << "decrypted '" << outp << "'\n";
}

int main() {

  toyclass t1;
  t1.encrypt();
  t1.decrypt();
  return 0;
}

K.

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.