c - 我应该如何用指针算术来解决这个密码?

标签 c pointers encryption pointer-arithmetic

问题如下:

Solve the following cipher using Pointer Arithmetic. The given string is GKQTEHIN. Define a pointer and set it to the letter ‘T’ in this string. Traverse the string forward and backward. Add/subtract a single value (key) between 1 and 5 to each letter to solve this cipher and form a meaningful word. Hint: You will be using this as you solve it.

希望这已经正确完成......

char given[]="GKQTEHIN";
char *pointer; 
pointer=&given[3];

pointer-=3;
printf("%c", *pointer-4);

pointer+=1;
printf("%c", *pointer+4);

pointer+=1;
printf("%c", *pointer-4);

pointer+=1;
printf("%c", *pointer-4);

pointer+=1;
printf("%c", *pointer+4);

pointer+=1;
printf("%c", *pointer+4);

pointer+=1;
printf("%c", *pointer-4);

pointer+=1;
printf("%c\n\n", *pointer+4);

打印出“COMPILER”一词。

最佳答案

How do I "add/subtract a single value...to each alphabet"?

计算机只能理解和处理数字,因为计算机中的一切都是数字(或可以被视为数字)。这意味着字母内部实际上是数字。 C 中的 char 使用 ASCII encoding ,这使得每个字母对应一个数字。因此,您只需将 key 添加到表示该 char 的代码中即可。您可以毫无问题地使用 char 作为数字。

char a = 'a';
a++;
printf("%c\n", a); // prints 'b'

关于c - 我应该如何用指针算术来解决这个密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19839068/

相关文章:

在运行时在函数中创建结构数组

mysql - Coldfusion 中的慢速加密

beagle board 交叉编译错误。 bash 错误是什么?

c - 洗一副纸牌,莫名其妙地崩溃了?

ios - 在 C 中分配/释放结构指针的动态数组

encryption - 盐可以防止字典或暴力攻击吗?

qt - QTcpSocket不会读取所有内容

c - 使用指针模拟简单函数的引用传递,但没有输出

c++ - 下面的数组和指针示例背后的逻辑是什么?

c - 程序有效,但无法理解警告