C语言:我输出的最后一个字符后有一个尾随字符
C language: there is a trailing character after the last character of my output
我正在为我的实验室制作凯撒密码 sheet,并使其能够加密 3 代入(凯撒密码),这是练习的重点。但是有一件事困扰着我。首先,如果我输入的不是 3,则有一个尾随字符。例如,输入 "malware",然后输入 2 作为键。
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char text[100];
int key,i;
printf("Please enter a word/sentence (lowercaps) for encrypting :\n ");
fgets(text,100,stdin);
printf("Please enter the key that you desire : eg:14\n");
scanf("%d", &key);
for(i=0;i<strlen(text);i++)
{
if (key>=26)
{
key=key%26;
}
if (text[i]==' ')
{
continue;
}
if(text[i]+key>'z')
{
text[i]-=97;
text[i]+=26;
text[i]+=key;
text[i]%=26;
text[i]+=97;
}
else
{
text[i]=text[i]+key;
}
}
printf("this is your encrypted text : %s", text );
}
希望我在编码时遵循了正确的缩进方法。因此受到了很多的反对
正如Blake_Lead所说,这个'\0'字符在你的密码中被改变了
事实上,我对缓冲区的长度是错误的,因为 fgets() 放了一个 '\0'
来自手册页:
A terminating null byte ('[=12=]') is stored after the last character in the buffer.
所以,你只需要改变你的测试
if (text[i]==' ')
通过类似的方式:
if (text[i] < 'A' || text[i] > 'z' || (text[i] > 'Z' && text[i] < 'a') )
我会将这段代码简化并更正为
#include <stdio.h>
int main() {
char text[100];
int key, i;
printf("Enter a word / sentence (lowercaps) for encrypting : ");
fgets(text, 100, stdin);
printf("Enter the key that you desire (eg. 14) : ");
scanf("%d", &key);
key %= 26; // Pull this out of the loop and remove the unnecessary if
for (i = 0; text[i]; ++i) { // Correct the loop condition
if (text[i] == ' ') continue;
if (text[i] + key > 'z')
text[i] = (text[i] - 97 + 26) % 26 + 97; // Simplify
else
text[i] += key;
}
printf("Encrypted text : %s\n", text);
return 0;
}
输入
Enter a word / sentence (lowercaps) for encrypting : malware
Enter the key that you desire (eg. 14) : 2
输出
Encrypted text : ocnyctg
代码是 1) 无法正确检测 char
何时为小写字母 2) 加密非字母,包括来自 fgets()
的 '\n'
,这导致 OP "trailing character after the last character of my output".
改为:
if (text[i] >= 'a' && text[i]<= 'z') {
text[i] = (text[i] - 'a' + key)%26 + `a`;
}
else {
; // nothing
}
或者
if (islower((unsigned char) text[i]) {
text[i] = (text[i] - 'a' + key)%26 + `a`;
}
注:以上依赖char
编码为ASCII.
不依赖ASCII的解决方案。
static const char lowercase[] = "abcdefghijklmnopqrstuvwxyz";
char *p = strchr(lowercase, text[i]);
if (p) {
int offset = (p - lowercase + key)%26;
text[i] = lowercase[offset];
}
我正在为我的实验室制作凯撒密码 sheet,并使其能够加密 3 代入(凯撒密码),这是练习的重点。但是有一件事困扰着我。首先,如果我输入的不是 3,则有一个尾随字符。例如,输入 "malware",然后输入 2 作为键。 这是我的代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char text[100];
int key,i;
printf("Please enter a word/sentence (lowercaps) for encrypting :\n ");
fgets(text,100,stdin);
printf("Please enter the key that you desire : eg:14\n");
scanf("%d", &key);
for(i=0;i<strlen(text);i++)
{
if (key>=26)
{
key=key%26;
}
if (text[i]==' ')
{
continue;
}
if(text[i]+key>'z')
{
text[i]-=97;
text[i]+=26;
text[i]+=key;
text[i]%=26;
text[i]+=97;
}
else
{
text[i]=text[i]+key;
}
}
printf("this is your encrypted text : %s", text );
}
希望我在编码时遵循了正确的缩进方法。因此受到了很多的反对
正如Blake_Lead所说,这个'\0'字符在你的密码中被改变了
事实上,我对缓冲区的长度是错误的,因为 fgets() 放了一个 '\0'
来自手册页:
A terminating null byte ('[=12=]') is stored after the last character in the buffer.
所以,你只需要改变你的测试
if (text[i]==' ')
通过类似的方式:
if (text[i] < 'A' || text[i] > 'z' || (text[i] > 'Z' && text[i] < 'a') )
我会将这段代码简化并更正为
#include <stdio.h>
int main() {
char text[100];
int key, i;
printf("Enter a word / sentence (lowercaps) for encrypting : ");
fgets(text, 100, stdin);
printf("Enter the key that you desire (eg. 14) : ");
scanf("%d", &key);
key %= 26; // Pull this out of the loop and remove the unnecessary if
for (i = 0; text[i]; ++i) { // Correct the loop condition
if (text[i] == ' ') continue;
if (text[i] + key > 'z')
text[i] = (text[i] - 97 + 26) % 26 + 97; // Simplify
else
text[i] += key;
}
printf("Encrypted text : %s\n", text);
return 0;
}
输入
Enter a word / sentence (lowercaps) for encrypting : malware
Enter the key that you desire (eg. 14) : 2
输出
Encrypted text : ocnyctg
代码是 1) 无法正确检测 char
何时为小写字母 2) 加密非字母,包括来自 fgets()
的 '\n'
,这导致 OP "trailing character after the last character of my output".
改为:
if (text[i] >= 'a' && text[i]<= 'z') {
text[i] = (text[i] - 'a' + key)%26 + `a`;
}
else {
; // nothing
}
或者
if (islower((unsigned char) text[i]) {
text[i] = (text[i] - 'a' + key)%26 + `a`;
}
注:以上依赖char
编码为ASCII.
不依赖ASCII的解决方案。
static const char lowercase[] = "abcdefghijklmnopqrstuvwxyz";
char *p = strchr(lowercase, text[i]);
if (p) {
int offset = (p - lowercase + key)%26;
text[i] = lowercase[offset];
}