Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Programming for the Absolute Beginner phần 7 pptx
MIỄN PHÍ
Số trang
35
Kích thước
15.0 MB
Định dạng
PDF
Lượt xem
791

Tài liệu đang bị lỗi

File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.

C Programming for the Absolute Beginner phần 7 pptx

Nội dung xem thử

Mô tả chi tiết

main()

{

char myString[21] = {0};

int iSelection = 0;

int iRand;

srand(time(NULL));

iRand = (rand() % 4) + 1; // random #, 1-4

while ( iSelection != 4 ) {

printf("\n\n1\tEncrypt Clear Text\n");

printf("2\tDecrypt Cipher Text\n");

printf("3\tGenerate New Key\n");

printf("4\tQuit\n");

printf("\nSelect a Cryptography Option: ");

scanf("%d", &iSelection);

switch (iSelection) {

case 1:

printf("\nEnter one word as clear text to encrypt: ");

scanf("%s", myString);

encrypt(myString, iRand);

break;

case 2:

printf("\nEnter cipher text to decrypt: ");

scanf("%s", myString);

decrypt(myString, iRand);

break;

case 3:

iRand = (rand() % 4) + 1; // random #, 1-4

printf("\nNew Key Generated\n");

174 C Programming for the Absolute Beginner, Second Edition

break;

} //end switch

} //end loop

} //end main

void encrypt(char sMessage[], int random)

{

int x = 0;

//encrypt the message by shifting each characters ASCII value

while ( sMessage[x] ) {

sMessage[x] += random;

x++;

} //end loop

x = 0;

printf("\nEncrypted Message is: ");

//print the encrypted message

while ( sMessage[x] ) {

printf("%c", sMessage[x]);

x++;

} //end loop

} //end encrypt function

void decrypt(char sMessage[], int random)

{

int x = 0;

Chapter 7 • Pointers 175

x = 0;

//decrypt the message by shifting each characters ASCII value

while ( sMessage[x] ) {

sMessage[x] = sMessage[x] - random;

x++;

} //end loop

x = 0;

printf("\nDecrypted Message is: ");

//print the decrypted message

while ( sMessage[x] ) {

printf("%c", sMessage[x]);

x++;

} //end loop

} //end decrypt function

SUMMARY

• Pointers are variables that contain a memory address that points to another variable.

• Place the indirection operator (*) in front of the variable name to declare a pointer.

• The unary operator (&) is often referred to as the “address of” operator.

• Pointer variables should always be initialized with another variable’s memory address,

with 0, or with the keyword NULL.

• You can print the memory address of pointers using the %p conversion specifier.

• By default, arguments are passed by value in C, which involves making a copy of the

incoming argument for the function to use.

• Pointers can be used to pass arguments by reference.

• Passing an array name to a pointer assigns the first memory location of the array to the

pointer variable. Similarly, initializing a pointer to an array name stores the first address

of the array in the pointer.

• You can use the const qualifier in conjunction with pointers to achieve a read-only

argument while still achieving the pass by reference capability.

176 C Programming for the Absolute Beginner, Second Edition

Tải ngay đi em, còn do dự, trời tối mất!