- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 C 的新手。我希望能够将字母表中的字母“x”移动多次以创建基本密码。
我在使用 islower() 函数时遇到问题。我正在使用“i”,但无法将其更改为字符。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
string p;
int main(int argc, string argv[])
{
//if argument count does not equal 2, exit and return 1
if (argc != 2)
{
printf("Less or more than 2 arguments given, exiting...\n");
return 1;
}
else //prompt user for plaintext to encrypt
{
p = GetString();
}
//take the second part of the array (the int entered by user) and store as k (used as the encryption key)
//string k = argv[1];
int k = atoi(argv[1]);
//function:
// c = (p + k) % 26;
//iterate over the characters in the string
//p represents the position in the alphabet of a plaintext letter
//c likewise represents a position in the alphabet
char new;
for (int i = 0, n = strlen(p); i < n; i++)
if (islower((char)i))
{
//printf("%c\n", p[i] + (k % 26));
printf("This prints p:%s\n", p);
printf("This prints i:%d\n", (char)i);
printf("This prints k:%d\n", k);
printf("This prints output of lower(i):%d\n", islower(i));
new = (p[i] - 97);
new += k;
//printf("%d\n", new %26 + 97);
//printf("i = |%c| is lowercase\n", i);
printf("%c\n", new % 26 + 97);
}
else {
//printf("%c", p[i] + (k % 26));
printf("This prints p:%s\n", p);
printf("This prints i:%d\n", (char)i);
printf("This prints k:%d\n", k);
printf("This prints output of lower(i):%d\n", islower(i));
new = (p[i] - 65);
new += k;
//printf("%d\n", new % 26 + 65);
//printf("i = |%c| is uppercase\n", i);
printf("%c\n", new % 26 + 65);
}
printf("\n");
}
输出:
jharvard@appliance (~/Dropbox/CS50x/pset2): ./caesar2 1
zZ < here is my input
This prints p:zZ
This prints i:0
This prints k:1
This prints output of lower(i):0
G < here is fails, lower case z should move to lower case a
This prints p:zZ
This prints i:1
This prints k:1
This prints output of lower(i):0
A < here is a success! upper case Z moves to upper case A
最佳答案
C 中使用的英文字母表是按 ASCII 定义的。 “Z”(ASCII 90)后仅跟有“{”(ASCII 91)。要返回“A”,您应该按以下方式进行所有转换:
请记住,这仅适用于英语的大写字母。因此,您可能希望使用 ctype.h
库中的 toupper()
。
如果您想为小字符添加类似的功能,请执行上述步骤,将 65 替换为 97。要检查您得到的是小字符还是大写字符,请使用 isupper()
。您必须为特殊字符添加更多特定代码。
关于C 凯撒密码 ASCII 字母换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22208139/
我刚刚开始学习 C 语言类(class),并且遇到了命令行参数的问题。分配是这样的(还有更多,但这是开头有关命令行参数的部分): - 你的程序必须接受一个命令行参数,一个非负整数。 - 如果您的程序在
我需要检查命令行参数中是否有非数字字符。例如: ./problem 20x 应该打印出“不是数字”,因为它包含一个 x。我的代码似乎没有循环遍历命令行参数中的所有字符。 我基本上尝试了不同类型的循环,
这里我有从标准输入将字符流输入到数组中的代码。然后将该数组转换为二维数组。然后,它将该数组从行列顺序更改为列行顺序。然后它打印出创建凯撒移位加密的新数组。我遇到的问题是我的数组开始使用第二个用户输入的
我有点被这个问题困住了。当我运行程序时,由于某种原因,循环经过 z 的所有字母都不会打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar
我是一名优秀的程序员,十分优秀!