简体   繁体   English

C语言:输出的最后一个字符后面有一个结尾字符

[英]C language: there is a trailing character after the last character of my output

I am making a Caesar's Cipher for my lab sheet, and have made it able to encrypt 3 subtitution(Caesar's Cipher), which is the point of the exercise. 我正在为自己的实验表制作一个凯撒密码,并使其能够加密3个替代(凯撒密码),这是练习的重点。 But there has been one thing bugging me. 但是有件事困扰着我。 First, there is a trailing character if i put it other than 3. For example, by typing "malware", and 2 for key. 首先,如果我输入的字符不是3,则有尾随字符。例如,通过键入“恶意软件”,并输入2作为密钥。 This is my code : 这是我的代码:

#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 );
}

I hope I followed the correct indentation methods for coding. 我希望我遵循正确的缩进方法进行编码。 Got a lot of dislikes because of that 因此有很多不喜欢的地方

Code is 1) not properly detecting when a char is a lower case letter 2) encrypting non-letters including '\\n' from fgets() which is causing OP's "trailing character after the last character of my output". 代码是1)无法正确检测char何时为小写字母2)加密来自fgets()包括'\\n'非字母,这会导致OP的“我输出的最后一个字符之后的尾随字符”。

Instead: 代替:

if (text[i] >= 'a' && text[i]<= 'z') {
   text[i] = (text[i] - 'a' + key)%26 + `a`;
}
else {
  ; // nothing 
}

Alternatively 另外

if (islower((unsigned char) text[i]) {
   text[i] = (text[i] - 'a' + key)%26 + `a`;
}

Note: the above depends on char are encoded as ASCII . 注意:以上依赖于char被编码为ASCII

A solution that does not depend on 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];
}

As Blake_Lead said, this '\\0' character was changed in your cypher 正如Blake_Lead所说,此“ \\ 0”字符已在您的密码中更改

Indeed I was wrong about the length of the buffer as fgets() puts a '\\0' 的确,由于fgets()将'\\ 0'放入缓冲区,我错了缓冲区的长度
From the manual page: 从手册页:

A terminating null byte ('\\0') is stored after the last character in the buffer. 终止的空字节(\\ 0)存储在缓冲区中的最后一个字符之后。

So, you just need to change your test 因此,您只需要更改测试

if (text[i]==' ')

by something like: 通过类似:

 if (text[i] < 'A' || text[i] > 'z' || (text[i] > 'Z' && text[i] < 'a') )

I will simplify and correct this code to 我将简化并更正此代码以

#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;
}

Input 输入

Enter a word / sentence (lowercaps) for encrypting : malware
Enter the key that you desire (eg. 14) : 2

Output 产量

Encrypted text : ocnyctg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM