Hi . I am coding the following program
i will give the string my self and one is already given.
given string = {a,b,c,d,3,f,g,h,i,6,j,k,l,m,9,n,o,p,q,2,r,s,t,u,v,w,x,y,z }, you are required to process an
input string possibly less than 80 characters long, and an input +ive integer to encrypt the
message: for example:
Entered Message: love you all.
+ive Yolo Integer: 3
Yolo Encryption: n2yh b2x dnn

I want to print the next 3rd alphabet of my input string after comparison with above string
the third alphabet after l is n
the third alphabet after o is 2 and all that ... My code is following but it is printing garbage . let me know and correct it please .

#include<iostream>
#include<string>
using namespace std;
int main(){
    
char str1[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    
char str2[80];
    
int i,n=2,result;
    
cout<<"Enter any string"<<endl;
    
cin>>str2;
    
cout << "String you enter is : " << str2 << endl;
	
for(i=0;i<=80;i++){
	if (strcmp( str1, str2)==0)
    str2[i+n];
 if(result==0){
 str2[i+n];
cout<<str1[i+n];
}

    
    
    system("pause");
    
    
    
    }

Recommended Answers

All 6 Replies

CORRECTNESS in code

#include<iostream>
#include<string>
using namespace std;
int main(){
    char str1[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char str2[80];
    int i,n=2,result;
    cout<<"Enter any string"<<endl;
    cin>>str2;
    cout << "String you enter is : " << str2 << endl;
	for(i=0;i<=80;i++){
	if (strcmp( str1, str2)==0)
    str2[i+n];
    cout<<str1[i+n];
}
 system("pause");
    }

Line 13 does nothing. Line 12 seems quite problematic (no null terminator for str1). Does line 12 seg fault?

Truth in labeling -- you need to give you variabless more descriptive names, for you and for us. Right now it's practically impossible to figure out what you're trying to do. Name your variables "alphabet", "decryptedString", "encryptedString", and "key" so everyone knows what's going on. "n", "str1", "str2" are hard to follow.

Count how many letters there are in the alphabet and make it a constant. To encrypt a letter, you find the index, add the key, and do some modular arithmetic. Loop through every letter. Encrypt a letter at a time.

What happened to the letter 'e'?

This letter is not included and i want to comparison any character of both strings then which character is compared i want to print its next third character or whatever number is given . i know there are some mistakes here . i am a new programmer and after some time it will clear .

If you can help then thanks . My mind is not working more than this .

Line 13 does nothing. Line 12 seems quite problematic (no null terminator for str1). Does line 12 seg fault?

Truth in labeling -- you need to give you variabless more descriptive names, for you and for us. Right now it's practically impossible to figure out what you're trying to do. Name your variables "alphabet", "decryptedString", "encryptedString", and "key" so everyone knows what's going on. "n", "str1", "str2" are hard to follow.

Count how many letters there are in the alphabet and make it a constant. To encrypt a letter, you find the index, add the key, and do some modular arithmetic. Loop through every letter. Encrypt a letter at a time.

What happened to the letter 'e'?

Hi I am a new programmer thats why i am here for some help kindly help me out about this program and i really don't know about encrypt and decrypt . thanks

>> i want to comparison any character of both strings

You can't. str1 isn't a string.

Let's do some renaming.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
	for(i=0;i<=80;i++){
	if (strcmp( alphabet, plainText)==0)
    plainText[i+key];
    cout<<encryptedText[i+key];
}
 system("pause");
    }

Now they have some descriptive names, you have all of the headers, and you have a start. Now remove the stuff that's way off, do a little formatting, and add a "return 0".

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
    for(i=0;i<=80;i++){
        // encryption code goes here
    }
    system("pause");
    return 0;
}

A little more code adding the appropriate null terminators for strings and a printout, plus you don't want to go through all 80 characters, only the relevant ones.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
    int length = strlen(plainText);
    // add a null terminator for encrypted text.  You need one.
    encryptedText[length] = 0;
    for(i=0;i < length;i++){
        // encryption code goes here.
        // isolate character i from plainText.  Calculate the corresponding
        // character using key and stick it in encrptedText[i]
    }

    // display encryptedText here
    system("pause");
    return 0;
}

key is 3 because you need to do something with 3 to get the appropriate corresponding encrypted letter.

Go through the example with paper and pencil, figure out EXACTLY how to get from plainText to encryptedText, then turn it into code.

You need to use precise names ("key", "plain", "raw", "encrypted", "decrypted"). Anyone working with encryption expects names similar to what I posted and the code, even when incorrect, is easier to follow when you use the correct terms. If you're completely new to encryption, I recommend putting down the C++ and doing a little studying on encryption to get the lingo down a little. Similarly, "string" has a very precise meaning in C++ and it isn't simply an array of characters. If you haven't studied strings, put this assignment on hold and learn them. One thing at a time.

>> i want to comparison any character of both strings

You can't. str1 isn't a string.

Let's do some renaming.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
	for(i=0;i<=80;i++){
	if (strcmp( alphabet, plainText)==0)
    plainText[i+key];
    cout<<encryptedText[i+key];
}
 system("pause");
    }

Now they have some descriptive names, you have all of the headers, and you have a start. Now remove the stuff that's way off, do a little formatting, and add a "return 0".

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
    for(i=0;i<=80;i++){
        // encryption code goes here
    }
    system("pause");
    return 0;
}

A little more code adding the appropriate null terminators for strings and a printout, plus you don't want to go through all 80 characters, only the relevant ones.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
    const int NUM_ALPHABET_CHARS = 29;
    char alphabet[]={'a','b','c','d','3','f','g','h','i','6','j','k','l','m','9','n','o','p','q','2','r','s','t','u','v','w','x','y','z' };
    char plainText[80];
    char encryptedText[80];
    int i,key=3,plainAlphabetIndex, encrpytedAlphabetIndex;
    cout<<"Enter any string that does not contain 'e' : "<<endl;
    cin>>plainText;
    cout << "Plain text you entered is : " << plainText << endl;
    int length = strlen(plainText);
    // add a null terminator for encrypted text.  You need one.
    encryptedText[length] = 0;
    for(i=0;i < length;i++){
        // encryption code goes here.
        // isolate character i from plainText.  Calculate the corresponding
        // character using key and stick it in encrptedText[i]
    }

    // display encryptedText here
    system("pause");
    return 0;
}

key is 3 because you need to do something with 3 to get the appropriate corresponding encrypted letter.

Go through the example with paper and pencil, figure out EXACTLY how to get from plainText to encryptedText, then turn it into code.

You need to use precise names ("key", "plain", "raw", "encrypted", "decrypted"). Anyone working with encryption expects names similar to what I posted and the code, even when incorrect, is easier to follow when you use the correct terms. If you're completely new to encryption, I recommend putting down the C++ and doing a little studying on encryption to get the lingo down a little. Similarly, "string" has a very precise meaning in C++ and it isn't simply an array of characters. If you haven't studied strings, put this assignment on hold and learn them. One thing at a time.

OK right but i don't know how to encrypt but i know a little bit programming as well .
But need a good tutor for this i will get soon . i don't know how to do that further you hint me thanks for that and more thanks for make me at least understand i will tell any other person to solve this and tought me encryption thanks ... if you can do further then thanks more for you.

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.