To the following program I need to:

Homework 2.b
/* crypto.c. Program that encrypts a letter by shifting the value
by k*/
#include <stdio.h>
#define BASE 65
#define RANGE 26
main()
{
char originalLetter;
char encryptedLetter;
int k;
printf("Input a Capital letter:\n");
scanf("%c", &originalLetter);
printf("Input an integer that is less than 26:\n");
scanf("%d", &k);
encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}

Task 1: Modify your program for homework 2.b so that your program will not do anything when the input is not a capital letter the shifting distance is not within the range 0~25

Task 2: Modify your program again so that your program can process both capital letters and lower case letters when the input is ‘a’ and k is 4, the output should be ‘e’ when the input is ‘A’ and k is 4, the output should be ‘E’
when the input is not a letter, your program do nothing when shifting distance is not within the range 0~25, your program also do nothing.

And then;

Task 1: Upgrade your program for homework 3.a, so that: the program can encrypt all the 95 displayable characters from ‘ !’ (33) to ‘~’ (126). The shifting distance now can range from 0 to 93. The program still checks if the input character and shifting distance are within the ranges

Task 2(counter-controlled loop): Ask the user to input the total number of characters to encrypt and shifting distance. Then, read in and encrypt the
characters one by one.

Task 3(sentinel-controlled loop): Ask the user to input a shifting distance. Then, read in any number of characters and encrypt them one by one until the user inputs ‘! ’

Recommended Answers

All 20 Replies

thank you for the info, but I read it already, but still need help on how to modify my program

Show us what you've tried.

Show us what you've tried.

For the first task I am adding an if structure but it won't work, but for me it seems like I have the right algorithm, I don't know what is wrong

/* crypto.c. Program that encrypts a letter by shifting the value
by k*/
#include <stdio.h>
#define BASE 65
#define RANGE 26
main()
{
char originalLetter;
char encryptedLetter;
int k;
printf("Input a Capital letter:\n");
scanf("%c", &originalLetter);
printf("Input an integer that is less than 26:\n");
scanf("%d", &k);

if ((originalLetter>=BASE && originalLetter<=90) && (k>=0 && k<=25))
{
originalLetter=BASE;

encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}

And for the second task I am putting on the if structure a bigger BASE with a value of 65 going up to 122

/* crypto.c. Program that encrypts a letter by shifting the value
by k*/
#include <stdio.h>
#define BASE 65
#define RANGE 26
main()
{
    char originalLetter;
    char encryptedLetter;
    int k;
    printf("Input a Capital letter:\n");
    scanf("%c", &originalLetter);
    printf("Input an integer that is less than 26:\n");
    scanf("%d", &k);

if ((originalLetter>=BASE && originalLetter<=122) && (k>=0 && k<=25))
{
    originalLetter=BASE;

    encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
    printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}

/* crypto.c. Program that encrypts a letter by shifting the value
by k*/
#include <stdio.h>
#define BASE 65
#define RANGE 26
main()
{
char originalLetter;
char encryptedLetter;
int k;
printf("Input a Capital letter:\n");
scanf("%c", &originalLetter);
printf("Input an integer that is less than 26:\n");
scanf("%d", &k);

if ((originalLetter>=BASE && originalLetter<=122) && (k>=0 && k<=25))
{
originalLetter=BASE;

encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */

Do you see my error somewhere??

Do you have a compile error or runtime error? If compile time error, it could just be your copy/paste, but you're missing the ending } .


Also, main should return an int. (Not returning an int in main is considered 'old style' )

Also, why are you setting your originalLetter = BASE? You end up setting it to A regardless of what you passed in...

if ((originalLetter>=BASE && originalLetter<=90) && (k>=0 && k<=25))
{
originalLetter=BASE;

encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}

Do you have a compile error or runtime error? If compile time error, it could just be your copy/paste, but you're missing the ending } .


Also, main should return an int. (Not returning an int in main is considered 'old style' )

Ok, I added the } at the end, it was saying: "Compound statement missing in function main(). now it is running .

about returning the int, the proffesor is showing us very slowly, because we don't know anything about programming, so I guess maybe that is why he is doing it the old style.

don't you always have to start with the first number, base on what tou are working on?

on my second task, if I input lower case a like, 'a' whith a shifting distance of 4, it won't give me 'e', it is giving me 'K'

I rewrote the program like this, but still don't get the right output

/* crypto.c. Program that encrypts a letter by shifting the value
by k*/
#include <stdio.h>
#define BASE 65
#define END 90
#define RANGE 26
#define SECBASE 97
#define SECEND 122
main()
{
char originalLetter;
char encryptedLetter;
int k;
printf("Input a Capital letter:\n");
scanf("%c", &originalLetter);
printf("Input an integer that is less than 26:\n");
scanf("%d", &k);

if ((originalLetter>=BASE && originalLetter<=END) && (k>=0 && k<=25))
{
encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}
else if((originalLetter>=SECBASE && originalLetter<=SECEND) && (k>=0 && k<=25))
{
encryptedLetter = SECBASE+(originalLetter-BASE+k)%RANGE;
printf("The encrypted letter is %c.\n", encryptedLetter);
}
}

I think I just got the second task done

/* crypto.c. Program that encrypts a letter by shifting the value by k*/
#include <stdio.h>
#define BASE 65
#define END 90
#define RANGE 26
#define SECBASE 97
#define SECEND 122
#define BASERANGE 0
#define ENDRANGE 25
main()
{
char originalLetter;
char encryptedLetter;
int k;
printf("Input a letter:\n");
scanf("%c", &originalLetter);
printf("Input an integer that is less than 26:\n");
scanf("%d", &k);

if ((originalLetter>=BASE && originalLetter<=END) && (k>=BASERANGE && k<=ENDRANGE))/*statement to follow for upper case character*/
{
encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the calculation*/
printf("The encrypted letter is %c.\n", encryptedLetter); /*Output */
}
if((originalLetter>=SECBASE && originalLetter<=SECEND) && (k>=BASERANGE && k<=ENDRANGE))/*statement to follow for lower case character*/
{
encryptedLetter = SECBASE+(originalLetter-SECBASE+k)%RANGE;/*the calculation for lower case*/
printf("The encrypted letter is %c.\n", encryptedLetter);
}

It's running just the way it should be running, :)

Now let me work on my task 1 for this program. It's amazing how one little thing like the } at the end of my program wasn't letting me finish

Today 06:41 PM
gmrod

Today 06:43 PM
gmrod

Today 06:48 PM
gmrod

Today 06:56 PM
gmrod

Today 07:08 PM
gmrod

Today 07:10 PM
gmrod

Much ado about nothing, I see. For future reference, you can edit your posts. It's generally a better practice than flooding the thread with pointless additions.

ok, thank you for your nice welcome to this phorum, :)
very helpful information, it's obviuos you are bless with the knowledge of programming and you use that as your weapon.

>thank you for your nice welcome to this phorum
Anytime, it's my pleasure. :)

>it's obviuos you are bless with the knowledge of programming
I worked very hard to learn everything I know. Stick to things you understand rather than try to diminish the effort it takes to learn how to program well. This is not a comment that I'll tolerate, either directed toward me or anyone else.

>and you use that as your weapon.
No, I use your mistakes as my weapon, and then only to get your attention. My knowledge of programming is what I use to help cure the rampant ignorance on this forum. That said, new people invariably think I'm being mean when I politely correct them for the first time. Rest assured, it only gets worse, but if you stick around, I can help you reach levels of programming knowledge that would otherwise require decades of experience.

You see, I have a different opinion than yours, and not that I am going to tell you that your beliefs are the wrong one, no no no. But I just think there are nicer ways to correct people mistakes.

And about
" worked very hard to learn everything I know. Stick to things you understand rather than try to diminish the effort it takes to learn how to program well. This is not a comment that I'll tolerate, either directed toward me or anyone else."
Not because you love to program those mean that everyone has to love it. I take this course because is a prerequirement for my other classes and is hard for somebody that doesn't like this kind of stuff to actually have to do it and learn it. I won't become and expert on the subject and i am not planning on do it either, but I do respect that you work hard for it, but sorry if the comment bothers you, but I still believe that you are blessed on programming

>You see, I have a different opinion than yours
I can live with that. You do things your way and I'll do them mine.

>Not because you love to program those mean that everyone has to love it.
I understand that, and I agree thoroughly. However, just because you don't care about programming doesn't mean that you can make light of the effort that others have put into it. It's like walking up to a career bodybuilder and saying "I see you were blessed with big muscles, how lucky of you to have them with no effort at all."

>but I still believe that you are blessed on programming
Your choice of wording is very poor. Blessed in this case would mean having good fortune or talent bestowed on me. That's far from the case as I'm not naturally inclined to programming, nor am I particularly talented at it. I worked very hard, and suffered years of frustration to get where I am. Every new concept I learn is a constant battle because I'm just not that smart. But I make up for it through dedication and hard work. Programming is not easy, it's especially difficult if you don't think like a programmer (like me), and I don't take kindly to people that suggest I was somehow gifted with amazing ability. Especially when using that implication as an insult.

that's the thing you are taking it as an insult, while I don't think is an insult. and sorry for my very poor wording, english is not my first language, if you want to talk in my native language I can give you excellent vocabulary while I write to you :)

Now if you excuse me, I will go and follow your recommendation and work hard on my homework, which I know are not difficult and that can be done by newbies like me. Hopefully you are not too mad and still be there to help out in case I need any help

>while I don't think is an insult
The weapon comment suggested an insult, so the resulting tone of your sentence was of sarcasm. If you didn't mean it that way then feel free to ignore any parts of my reply that you don't like. Everyone else does.

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.