2016-04-21 44 views
1

iam试图用C编写一个简单的凯撒程序。C中的凯撒加密,带有未定义的输入

我想给出的输入应该与程序中的未定义长度一样。

我遇到的问题是,当我在加密的字符串中输入一个像“HELLO WORLD”这样的短字符串时,我在结尾处有一些随机添加。 当我给一个更长的字符串作为输入时,程序崩溃了,它开始加密。

我不知道在哪里的问题可能是,也许有些你可以给我一个手或有想法有什么废话位置:/

每一个建议是欢迎谢谢:)

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

// realloc the size of memory for the user input 
char *inputString(FILE *fp, size_t size) 
{ 
    // the size is extendet by the input with the value of the provisional 
    char *str; 
    int ch = 0; 

    size_t len = 0; 
    str = realloc(NULL, sizeof(char)*size); // size is start size 

    if(!str) 
    return str; 

    while(EOF != (ch = fgetc(fp)) && ch != '\n') 
    { 
    str[len++] = ch; 
    if(len == size) 
    { 
     str = realloc(str, sizeof(char)*(size += 16)); 
     if(!str) 
     return str; 
    } 
    } 
    str[len++] = '\0'; 
    return realloc(str, sizeof(char)*len); 
} 


// lowercase letters 
void makeLowerCase(char *input) 
{ 
    while(*input != '\0') // "while(*input)" would also work 
    { 
    *input = tolower(*input); 
    input++; 
    } 
} 

// test function 
// character length of the input 
int lengthOfInput(char *input) 
{ 
    int len = 0; 
    while(*input != '\0') // "while(*input)" would also work 
    { 
    len++; 
    input++; 
    } 
    return len; 
} 


// encrypts the input caesar 
char *encrypt(char *toBeEncrypted, int caesarNum) 
{ 
    int tmp = 0; // going through the input 
    char *tbE; 

    for(;*toBeEncrypted != '\0'; toBeEncrypted++, tmp++) 
    { 
    *(tbE+tmp) = *toBeEncrypted + caesarNum; 
    printf("%c", *(tbE+tmp)); 
    } 
    return tbE; 
} 


// ------------------------------------------------------ 
//      main 
// ------------------------------------------------------ 
int main(void) 
{ 
    char *s; // input from user 
    char *encryptedString; // encrypted input/output 
    int loi = 0; // length of input 

    srand((unsigned)time(NULL)); // initialize random value for encryption 
    int caesar = (rand()%25) + 1; // random number for caesar encryption 

    printf("input string : "); 
    s = inputString(stdin, 10); // realloc the memory space for the input 

    makeLowerCase(s); // convert to lower case 
    printf("lower case: %s\n",s); 

    loi = lengthOfInput(s); // length of the input 
    printf("lenght of input: %d\n",loi); 
    printf("caesar number: %d\n",caesar); 

    encryptedString = encrypt(s, caesar); // encryption 
    printf("\nencrypted : %s",encryptedString); // output 

    free(s); 
    free(encryptedString); // free the memory 
    return 0; 
} 
+0

'malloc' /'realloc'记忆'TBE ' –

+0

感谢您的建议,我已将其更改至目前为止 – A7GPS0

+0

您忘记了NUL终结者。 'char * tbE = malloc(sizeof(char)* lOI);'→'char * tbE = malloc(sizeof(char)*(lOI + 1));'和'return tbE;'之前,添加'tbE [lOI ] ='\ 0';'或'tbE [i] ='\ 0';'。此外,我已经回滚了你的编辑,因为它使我的评论以及迈克尔的回答毫无意义。 –

回答

2

您正在使用未初始化的变量。

// encrypts the input caesar 
char *encrypt(char *toBeEncrypted, int caesarNum) 
{ 
    int tmp = 0; // going through the input 
    char *tbE; 

    for(;*toBeEncrypted != '\0'; toBeEncrypted++, tmp++) 
    { 
    *(tbE+tmp) = *toBeEncrypted + caesarNum; 
// ^
// ^here tbE is used, but it has never been initialized 
// 
    printf("%c", *(tbE+tmp)); 
    } 
    return tbE; 
} 


可能还有更多的问题。

边注

encrypt环路应与数组索引被写入,而不是与指针运算为可读性起见:

for(i = 0; toBeEncrypted[i] != '\0'; i++) 
{ 
    tbE[i] = toBeEncrypted[i] + caesarNum; 
    printf("%c", tbE[i]); 
} 
+0

感谢您的快速回复。 释放内存我以为我必须释放他们,因为我必须“不同”的指针。 它是不相关的他们我自由 - s或enryptedString? – A7GPS0

+0

@ A7GPS0如果将其初始化为NULL,它会崩溃。你必须用'malloc'分配内存。 –

+0

除了可读性,如果我用数组而不是指针算术写它,有什么优点吗? – A7GPS0