卡住了Caesar.c [英] Got stuck with Caesar.c

查看:77
本文介绍了卡住了Caesar.c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从edx编程简介中运行程序分配caesar.c.它需要一个能够使用Caesar加密对字符串进行加密的程序:因此,用户必须输入密钥(命令行);例如,使用密钥2时,需要将"A"字符加密为"C"字符;当您必须输入一个大于26的键(字母字母的数量)时,问题就开始了.例如,对于27的键和"A"字符,程序必须像1的键一样返回"B".

I am trying to run the program assignment caesar.c from the edx Introduction to programming. It requires a program able to encrypt a string with the Caesar encryption: therefore, the user has to enter a key (command-line); for example with a key of 2 a 'A' character needs to be encrypted in a 'C' character; the problem starts when you have to enter a key greater than 26, which is the number of alphabetical letters. For a key of 27 and an 'A' character for example, the program must return 'B' like a key of 1.

我试图将字符的ASCII值转换为从0到26的字母值,以便在键等于或大于26时使用模数运算符.它给我一个分段错误.有人可以帮我提供一些导致错误的原因的建议吗?

I have tried to transform the ASCII values of the characters to alphabetical values from 0 to 26 in order to use the modulus operator when the key is equal or greater than 26. It returns me a segmentation fault. Can anyone help me with some suggestions of the causes of my error?

这是程序:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int key;

// function for an alphabetic value with non capital letters

int alpha_low( char c )
{
    int alpha_value;
    alpha_value = (int) c - 97;
    return alpha_value + ( key % 26 );
}

// function to return to ascii valuee for non capital letters

char ascii_low( char c )
{
    return (char) alpha_low( c ) + 97;
}

// function for an alphabetic value with capital letters

int alpha_up( char c )
{
    int alpha_value;
    alpha_value = (int) c - 65;
    return alpha_value + ( key % 26 );
}

// function to return to ascii value for capital letters

char ascii_up( char c )
{
    return (char) alpha_up( c ) + 65;
}


int main(int argc, string argv[])
{
        int result;
        string p;
        key = atoi( argv[1] );

    if( argc != 2 || key < 0 )
    {
       printf("Usage: ./caesar key(positive integer)\n");
       return 1;
    }

    printf("Please, write a plaintext: ");
    p = GetString();

    for( int i = 0, n = strlen(p); i < n; i++)
    {
       if ( isalpha(p[i]) )
       {
          if ( islower(p[i]) )
          {
             result = alpha_low( p[i] );
             printf("%c", ascii_low( p[i] ));
          }
          else if( islower( p[i]) )
          {
              result = alpha_up( p[i] );
              printf("%c", ascii_up( p[i]) );
          }
        }  
    }      

    return 0;
} 

推荐答案

凯撒字母char的函数应类似于(在基本步骤中分解):

A function to caesar an alphabetic char should be like (decomposed in elementary steps):

int caesar_lower(int c,int key) {
    int v = c-'a'; // translate 'a'--'z' to 0--25
    v = v+key;     // translate 0--25 to key--key+25
    v = v%26;      // translate key--key+25 to key--25,0--key-1
    v = v+'a';     // translate back 0--25 to 'a'--'z'
    return v;
}

这篇关于卡住了Caesar.c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆