0

Similar Question asked

I read thru the solution(See link above) and followed the instructions to remove the
' If-Else ' statements ,But I don't understand why it works (?)

Could someone explain why it happens . . .

There is this final error I'm struggling to place the \n in printf statement I could use a hint here ...

Thank you,

Code with spacing error in output


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

int main(int argc, char* argv[])
{
    int i=0,n=0,j=0;  
    if(argc!=2)   
    {
      printf("Bad I/p");
      return 1;
    } 

    char* key=argv[1];   
    n=strlen(key);

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

    char* msg=GetString();
    int m=strlen(msg);

    if(isalpha(msg[j]))
    {     
        int t=0;
        for(j=0;j<m;j++)
        {               
            t=j%n;
            if(isupper(msg[j]))
            {                   
                if(isupper(key[t]))
                {  
                    // k%n = k% strlen(keyword) cipher formula for upper case
                    msg[j]=(((msg[j]-65)+(key[t]-65))%26)+65;    
                    printf("%c",msg[j]);
                }//upperkey

                if(islower(key[t]))
                {                   
                    // k%n = k% strlen(keyword) cipher formula for upper case
                    msg[j]=(((msg[j]-65)+(key[t]-97))%26)+65;    
                    printf("%c",msg[j]);
                }//lowerkey

            }//uppermsg

            if(islower(msg[j]))                     
            {                       
                if (isupper(key[t]))     
                {  
                    // k%n = k% strlen(keyword) cipher formula for upper case
                    msg[j]=(((msg[j]-97)+(key[t]-65))%26)+97;                       
                    printf("%c",msg[j]);                        
                }//upperkey

                if(islower(key[t]))                         
                {
                    // k%n = k% strlen(keyword) cipher formula for upper case
                    msg[j]=(((msg[j]-97)+(key[t]-97))%26)+97;
                    printf("%c",msg[j]);                            
                }//keylower 

            }//islowermsg 
            t++;

        }//for loop bracket

        printf("\n");  

    } //isalpha

}

Thank you

Here is the error msg displayed now enter image description here

1 Answer 1

1

You have two immediate problems.

First, the code doesn't process non-alpha characters (numbers, punctuation, etc.) It just abandons them. They have to be passed through and printed in the same position.

Second, you are using the same counter to track the characters in the string to be encoded and to track the key string. Because the code does this, the key gets out of synch with the text to be encoded. The spec says that you only advance to the next key character after processing a letter. In other words, when you process a non-alpha character, you don't advance to the next character in the key.

BTW, incrementing t at the end of the loop doesn't really do anything because you set it to j at the beginning of every pass.

FIx these two issues and you'll be a long way down the road.

As for the error about not handling the key with a number in it, I suspect that's about check50 expecting exact output. Go back and check the spec and compare that to what your code does.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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