char数组中的杂散字符

时间:2014-10-11 08:35:54

标签: c malloc

我遇到此代码的问题,此代码是对铁路密码的加密,如果您输入“测试”输入,您应该得到输出“tietnsg”,我得到。

但是,如果我将输入更改为“testingj”,我会得到“tietnjsgp?”的输出!lj“我可以从调试中看到”?²!lj“似乎在最后一次填充期间被标记为toCipher函数 有没有人知道如何修复它除了我做它的方式?

/*
    CIS Computer Secutrity Program 1
    10-10-14
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

char *toCipher(char **arr,int x,int y);
char *Encrypt(char *pT, int size);
char **create(int x,int y);
void FreeArr(char **array, int y);
void print(char *word,int strl);

int main(){
    char pt[]= "testingj";
    char *word = Encrypt(pt,3);
    print(word, sizeof(pt));
    free(word);

}

/*
Take in a pointer to a word, and the lenght of the string
Post print each char in the array, (used beacuase i had some issues with the memory, i keep getting extra adresses
*/
void print(char *word,int strl){
    int i;
    for(i=0;i<strl-1;i++){
        printf("this is correct %c",word[i]);
    }
    printf("\n");
}

/*
Pre, take in the pointer to the plain text word to be encrypted as well as the depth of the Encryption desired
Post: Construct the array, insert values into the 2d array, convert the 2d array to a 1d array and return the 1d array
*/

char *Encrypt(char *word,int y){
    int x = strlen(word);
    int counter=0;
    int ycomp=0;
    int rate=1;

    char **rail = create(x,y);

        while(counter<x){   
            if(ycomp==y-1){
                rate=-1;
            }
            if(ycomp==0){
                rate=1;         
            }
            rail[counter][ycomp]=word[counter]; 
            ycomp=ycomp+rate;
            counter++;
        }//end of rail construction

    char *DrWord = toCipher(rail,x,y);  

    FreeArr(rail,y);    
    return(DrWord);
}


/*
Create a dynamic 2d array of chars for the rail cypher to use
Take in the dimensions
return the pointer of the rails initial address, after it created the space for the rail
*/


char *toCipher(char **arr,int x,int y){
    int xI =0;
    int yI=0;
    int counter =0;
    char *word = (char*)malloc(x);
    int i;


    for(yI=0;yI<y;yI++){    
        for(xI=0;xI<x;xI++){
            if(arr[xI][yI]!= 0){                    
                word[counter]=arr[xI][yI];
                counter++;
            }
        }

    }
        printf("this is the problem %s\n",word);
        return(word);
}




char **create(int x, int y){
    char **rail;            
    int i,j;

    rail = malloc(sizeof(char**)*x);

    for(i=0;i<x;i++){
        rail[i]= (char*)malloc(y * sizeof(char*));
    }

    for(i=0;i<y;i++){
        for(j=0;j<x;j++){
            rail[j][i]= 0;
        }
    }

    return(rail);
}


/*
Pre, take in a malloc'd array, with the height of the array 
free the malloc calls one by one, and finally free the initial adress
*/
void FreeArr(char **array, int y){
    int i;
    for(i=0;i<y;i++){
        free(array[i]);
    }
    free(array);
}

2 个答案:

答案 0 :(得分:1)

toCipher中,word打印时没有空终止。之一:

char *word = (char*)malloc(x+1); // allocate an extra char for nul.
word[x] = 0; // add the nul at the end.

或:

printf("this is the problem %.*s\n",x,word); // limit characters printed to x.

答案 1 :(得分:0)

我忘了将单词初始化为0,如果你在调试模式下看它,标记的内存没有被替换,因此在新构造的字符串中标记了