1

My code complies well but encrypts the text in the wrong way.Could someone help me find the problem?

any help will be appreciated

here is my code:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, string argv[])
{
if (argc != 2)
{
     printf("Please enter a valid key\n");
    return 1;
}

string key = argv[1];

for (int i=0 ,n=strlen(key);i<n; i++)
{
    if(!isalpha(key[i]))
    {
        return 1;
    }
}

printf("input:");

string text = GetString();
int k = 0;

for(int i = 0,n = strlen(text),j = k % strlen(key); i < n ; i++,j++)
{


    if(isupper(text[i]))
    {
        char c = ((text[i]-65 + key[j])%26) + 65;
        printf("%c",c);
    }

    else if (islower(text[i]))
    {
        char c = ((text[i]-97 + key[j])%26) + 97;
        printf("%c",c);
    }

    else
    {
        printf("%c",text[i]);
    }

}
 printf("\n");
 return 0;
}
0
2

Your code has several issues. First, the way that you set the for loop up (incrementing both I and j on every pass) means that no matter what, the index for the key, j, is going to increment for every character in the plaintext string, whether it is a letter or a non-alpha. This is bad. It means that the key will get out of synch as soon as a non-alpha is processed. The key index should ONLY be incremented when a letter is actually encoded, and reset when it gets too big (you should be able to figure out what too big implies.) Because of this, it should NOT be part of a loop construct. A simple var can handle the job nicely.

Next, your encoding has multiple errors. You are correctly subtracting 'a' or 'A' from the letter to be encoded. Think about the reason for doing this and then ask why you aren't also doing it for the key letter? Next, what is the purpose of the %26 and why do you apply that to the key only? This is not as simple as you think. You should really go back and review the videos that cover this.

Have you thought about the case (upper/lower case) of the key letter?

Finally, because of an issue with the pset instructions, I'll give you this one. The prompt for input, printf("input:"); will cause a check50 error. The instructions say to prompt for input, but then give an example of how the program should behave, which doesn't actually prompt for input, it just waits for input. The latter is what you should be doing. (This is being looked at for correction.)

This should get you going. IF this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • thank you for your advice but ..how would i go about about incrementing j only when a character has been encrypted ?
    – Burugux
    Feb 9 '16 at 12:47
  • Very simply. Remove j from the for loop setup, and when you encode a letter, j++; Of course, you need to make sure that you reset it to 0 when it hits the end of the key. There's a hidden lesson here - don't overcomplicate things when you don't have to. Keep it simple.
    – Cliff B
    Feb 14 '16 at 0:42
1

Your formula is wrong... Think about it again... You are on the right path but you are missing some steps in it... Try to encrypt a code yourself maybe of 2 to 3 characters.... Try it and let me know if you still dont get it.

3
  • i still don't get it unfortunately ...i trying encrypting two characters and the results am getting are way off.What could be the problem?
    – Burugux
    Feb 8 '16 at 11:40
  • I will give you a hint to one of the problems. In the problem set text you find this line: if your code is about to apply the jth character of k to the ith character of p, but the latter proves to be a non-alphabetical character, you must wait to apply that jth character of k to the next alphabetical character in p; you must not yet advance to the next character in k_
    – Locrin
    Feb 8 '16 at 16:43
  • Okay another hint : %26.. One of the steps you need to add and there is one more ... But please try to understand the problem fully before adding anything... You might find some ither way to do it..Beauty of programming. Feb 8 '16 at 19:27
1

Without giving it away entirely, I can tell you there are a couple places where you should focus your attention.

  1. How you increment j. Part of the challenge of the logic of this pset is seeing how i and j are different in terms of how they change. The pset specs/walkthrough should provide some help here.

  2. How you treat key[j] in the two if conditions of your for loop. Go through GDB and/or write out values by hand. The challenge here is to move back and forth from ASCII values to alphabetical indexes. That, ultimately, is a fundamental part of this pset's challenge.

I suggest re-watching Zamyla's walkthrough video and writing out some work by hand. Using a short and easy key like "aaaaa" for something simple like "abcde" will help. Figure it out on paper/with pseudocode first, and the logic of the program should follow.

0

I am a bit confused about this part. The k never changes so, lets say key is house, the strlen(house) is 5. But 0% 5 stays the same for the whole loop. J starts to change, but it just keeps growing with every loop, there is no condition set. You should use the module. Lets say house is the key, Meet me at park phrase. For the first 5 letters it should work, but after that j becomes 5 so key[5]= .. nothing, house has key[4] = 'e'. See if that helps. j%strlen(key) might be helpful.

int k = 0;   
j = k % strlen(key)
1
  • when i went through my code again ..i realised this and i was able to correct it thankyou
    – Burugux
    Feb 15 '16 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .