#include <stdio.h>
int main()
#include <stdio.h>
int main()
{
char msg[31] = {'\0'};
char encrypted[31] = {'\0'};
int key;
printf("Please enter a message under 30 characters: ");
fgets(msg, 31, stdin);
printf("Please enter an encryption key: ");
scanf("%d", &key);
int i = 0;
while (msg[i] && ('a' <= msg[i] <= 'z' || 'A' < msg[i] < 'Z'))
{
encrypted[i] = (msg[i] + key);
i++;
}
printf("%s\n", msg);
printf("%d\n", key);
printf("%s\n", encrypted);
}
Okay i've got my code to increment the characters but i don't know how to make it ignore special characters and spaces. Also how do i use %
to loop back to 'a'
and 'A'
?
好的,我有我的代码来增加字符,但我不知道如何使它忽略特殊字符和空格。另外我如何使用%循环回'a'和'A'?
Thank you.
3 个解决方案
#1
0
-
You just need a simple
for
loop:你只需要一个简单的for循环:
for (int i = 0; i < 31; i++) { // operate on msg[i] }
If you didn't know the length of the string to begin with, you might prefer a
while
loop that detects the null terminator:如果你不知道字符串的长度,你可能更喜欢检测空终止符的while循环:
int i = 0; while (msg[i]) { // operate on msg[i] i++; }
-
Your
fgets
andscanf
are probably fine, but personally, I would be consistent when reading input, andfgets
for it all. Then you cansscanf
to getkey
out later.你的fgets和scanf可能很好,但就个人而言,我会在阅读输入时保持一致,并为此付出一切代价。然后你可以sscanf稍后获取密钥。
#2
0
scanf
and fgets
seem fine in this situation the way you've used them.
scanf和fgets在这种情况下看起来很好用你使用它们的方式。
In C, a string is just an array of characters. So, you access each element using a for
loop and array indexing:
在C中,字符串只是一个字符数组。因此,您使用for循环和数组索引访问每个元素:
for (int i = 0; i < strlen(str); i++) {
char thisChar = str[i];
//Do the processing for each character
}
You can perform arithmetic on thisChar
as necessary, but be careful not to exceed 255. You might want to put a check on key
to ensure it doesn't get too big.
您可以根据需要对thisChar执行算术运算,但请注意不要超过255.您可能需要检查密钥以确保它不会变得太大。
#3
0
Getting a string from scanf:
从scanf获取字符串:
char msg[31];
scanf("%30s", msg);
OR (less efficient, because you have to fill the array with 0s first)
或者(效率较低,因为你必须首先用0填充数组)
char msg[31] = { 0 };
scanf("%30c", msg);
Iterating a string is as easy a for loop (be sure to use c99 or c11)
迭代字符串就像循环一样简单(一定要使用c99或c11)
int len = strlen(msg);
for(int i = 0; i < len; i++) {
char current = msg[i];
//do something
msg[i] = current;
}
"Encrypting" (i.e. ciphering) a character require a few steps
“加密”(即加密)一个角色需要几个步骤
- Determine if we have an uppercase character, lowercase character, or non-alphabetic character
- Determine the position in the alphabet, if alphabetic.
- Update the position, using the modulus operator (%)
- Correct the position, if alphabetic
确定我们是否有大写字符,小写字符或非字母字符
如果是字母,则确定字母表中的位置。
使用模数运算符更新位置(%)
如果是字母,请更正位置
I could give you the code here, but then you wouldn't learn anything from doing it yourself. Instead, I encourage you to implement the cipher based on the steps I provided above.
我可以在这里给你代码,但是你不会自己学习任何东西。相反,我鼓励您根据我上面提供的步骤实现密码。
Note that you can do things like:
请注意,您可以执行以下操作:
char c = 'C';
char e = 'E' + 2;
char lower_c = 'C' - 'A' + 'a';
#1
0
-
You just need a simple
for
loop:你只需要一个简单的for循环:
for (int i = 0; i < 31; i++) { // operate on msg[i] }
If you didn't know the length of the string to begin with, you might prefer a
while
loop that detects the null terminator:如果你不知道字符串的长度,你可能更喜欢检测空终止符的while循环:
int i = 0; while (msg[i]) { // operate on msg[i] i++; }
-
Your
fgets
andscanf
are probably fine, but personally, I would be consistent when reading input, andfgets
for it all. Then you cansscanf
to getkey
out later.你的fgets和scanf可能很好,但就个人而言,我会在阅读输入时保持一致,并为此付出一切代价。然后你可以sscanf稍后获取密钥。
#2
0
scanf
and fgets
seem fine in this situation the way you've used them.
scanf和fgets在这种情况下看起来很好用你使用它们的方式。
In C, a string is just an array of characters. So, you access each element using a for
loop and array indexing:
在C中,字符串只是一个字符数组。因此,您使用for循环和数组索引访问每个元素:
for (int i = 0; i < strlen(str); i++) {
char thisChar = str[i];
//Do the processing for each character
}
You can perform arithmetic on thisChar
as necessary, but be careful not to exceed 255. You might want to put a check on key
to ensure it doesn't get too big.
您可以根据需要对thisChar执行算术运算,但请注意不要超过255.您可能需要检查密钥以确保它不会变得太大。
#3
0
Getting a string from scanf:
从scanf获取字符串:
char msg[31];
scanf("%30s", msg);
OR (less efficient, because you have to fill the array with 0s first)
或者(效率较低,因为你必须首先用0填充数组)
char msg[31] = { 0 };
scanf("%30c", msg);
Iterating a string is as easy a for loop (be sure to use c99 or c11)
迭代字符串就像循环一样简单(一定要使用c99或c11)
int len = strlen(msg);
for(int i = 0; i < len; i++) {
char current = msg[i];
//do something
msg[i] = current;
}
"Encrypting" (i.e. ciphering) a character require a few steps
“加密”(即加密)一个角色需要几个步骤
- Determine if we have an uppercase character, lowercase character, or non-alphabetic character
- Determine the position in the alphabet, if alphabetic.
- Update the position, using the modulus operator (%)
- Correct the position, if alphabetic
确定我们是否有大写字符,小写字符或非字母字符
如果是字母,则确定字母表中的位置。
使用模数运算符更新位置(%)
如果是字母,请更正位置
I could give you the code here, but then you wouldn't learn anything from doing it yourself. Instead, I encourage you to implement the cipher based on the steps I provided above.
我可以在这里给你代码,但是你不会自己学习任何东西。相反,我鼓励您根据我上面提供的步骤实现密码。
Note that you can do things like:
请注意,您可以执行以下操作:
char c = 'C';
char e = 'E' + 2;
char lower_c = 'C' - 'A' + 'a';