C语言-if语句问题

Posted

技术标签:

【中文标题】C语言-if语句问题【英文标题】:C language-if statement issue 【发布时间】:2016-11-30 02:28:49 【问题描述】:

我创建了以下程序来制作凯撒和维吉纳密码。我已经让 Caesar 密码正常工作,但是我无法让 Vigenere 正常工作。

我想要发生的是让我的 if 语句以 5 的间隔“捕获”所有各种数字。但是,每当我运行程序时,我的 Viegenere 密码输出完全相同的输入。我相信这是因为我的 if 语句有误,但我不确定它是什么。

Viegenere cipher 的开头在代码中注释为 //Vigenere Cipher--keyword is "apple"

#include <stdio.h>


int main()
int i=0;
 //setting the individual slot number for the array-- later used in the while loop
char guy[100];
printf("Enter the plain text:");
fgets(guy,100,stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right?

while (guy[i] != '\0') //while loop that runs until it reaches the end of the string


    if ((guy[i]) >= 'A' && (guy[i]<= 'Z')) //moves capital letter values up 1
        if (guy[i]=='Z')
            guy[i]=guy[i]-25;
        
        else 
        guy[i]=guy[i]+1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98
        
        
    if ((guy[i]) >= 'a' && (guy[i]) <= 'z')// moves lower case letter values up 1
        if (guy[i]=='z')
            guy[i]=guy[i]-25;
        
        else
        guy[i]=guy[i]+1;
        
    
    i++; //moves the array's interval up to the next "slot"


printf("Encrypted text is: %s\n",guy);

//Vigenere Cipher-- keyword is "apple"
//a = 1  value shift
//p = 16 value shift
//p = 16 value shift
//l = 17 value shift
//e = 5  value shift

printf("Enter the plain text: ");
fgets(guy,100,stdin);//takes user's input

while (guy[i] != '\0') //while loop that runs until it reaches the end of the string

    if (i%5==0 || i==0) //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1
    guy[i] = guy[i]+1;
    

    if ((i-1)%5==0 || i==1) //all numbers that are second in the key word 'apple', such as 1,6,11,16
        guy[i]=guy[i]+16;
    
    if ((i-2)%5==0 || i==2)// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22
        guy[i]=guy[i]+16;
    
    if((i-3)%5==0 || i==3)// all numbers that are fourth to the key word 'apple', such as 3,8,13,18
        guy[i]=guy[i]+17;
    
    if((i-4)%5==0 || i==4)// all numbers that are fifth in the key word 'apple', such as 4,9,14,19
        guy[i]=guy[i]+5;
    
    else 
    i++;
    
    
printf("Encrypted text is: %s\n",guy);

【问题讨论】:

我无法重现您的错误。但是我没有使用标准输入法,所以也许你在某个地方有一个导致未定义行为的错误。我建议你对明文进行硬编码,看看问题是否仍然存在。 l = 12 值移位。并使用% 您应该阅读更多关于模运算符的信息。 ((i-3)%5==0 || i==3)(i%5 == 3 || i == 3) 相同,与(i%5==3)相同。 【参考方案1】:

在再次加密数据之前 - 你应该重新初始化“i”变量的值

#include <stdio.h>


    int main()
    int i = 0;
    //setting the individual slot number for the array-- later used in the  while loop
    char guy[100];
    printf("Enter the plain text:");
    fgets(guy, 100, stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right?

    while (guy[i] != '\0') //while loop that runs until it reaches the end of the string


        if ((guy[i]) >= 'A' && (guy[i] <= 'Z')) //moves capital letter values up 1
            if (guy[i] == 'Z')
                guy[i] = guy[i] - 25;
            
            else 
                guy[i] = guy[i] + 1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98
            
        
        if ((guy[i]) >= 'a' && (guy[i]) <= 'z')// moves lower case letter values up 1
            if (guy[i] == 'z')
                guy[i] = guy[i] - 25;
            
            else
                guy[i] = guy[i] + 1;
            
        
        i++; //moves the array's interval up to the next "slot"

    
    printf("Encrypted text is: %s\n", guy);

    //Vigenere Cipher-- keyword is "apple"
    //a = 1  value shift
    //p = 16 value shift
    //p = 16 value shift
    //l = 17 value shift
    //e = 5  value shift
    printf("The value of i = %d\n", &i);
    i = 0;
    printf("Enter the plain text: ");
    fgets(guy, 100, stdin);//takes user's input

    while (guy[i] != '\0') //while loop that runs until it reaches the end of the string

        if (i % 5 == 0 || i == 0) //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1
            guy[i] = guy[i] + 1;
        

        if ((i - 1) % 5 == 0 || i == 1) //all numbers that are second in the key word 'apple', such as 1,6,11,16
            guy[i] = guy[i] + 16;
        
        if ((i - 2) % 5 == 0 || i == 2)// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22
            guy[i] = guy[i] + 16;
        
        if ((i - 3) % 5 == 0 || i == 3)// all numbers that are fourth to the key word 'apple', such as 3,8,13,18
            guy[i] = guy[i] + 17;
        
        if ((i - 4) % 5 == 0 || i == 4)// all numbers that are fifth in the key word 'apple', such as 4,9,14,19
            guy[i] = guy[i] + 5;
        
        else 
            i++;
        
    
    printf("Encrypted text is: %s\n", guy);
    getchar();

输出:-

输入纯文本:apple 密文为:bqqmf

i 的值 = 1900200 - 重新初始化前 i 的值。

输入纯文本:apple 加密文本为:bÇÇ

【讨论】:

它不应该超过 z。

以上是关于C语言-if语句问题的主要内容,如果未能解决你的问题,请参考以下文章

c语言中的 条件语句 if else

c语言中的条件语句if

C语言中三个if语句的嵌套怎理解

C语言问题,我想的是if条件语句里三个都满足,但是只能满足两个,有啥办法解决,急,在线等?

C语言。。。if语句

C语言:if语句 一元二次方程