2017-06-21 14 views
-2

enter image description hereだから、単語をスキャンして所定のキーで暗号化するプログラムが必要です(キーはメインにあります)。 出力は暗号化された単語で構成されている必要があります(暗号化は単語の文字のASCII値を持つキーを使用しているので、最初の文字はBOOKとし、65文字目のキーに切り替わります)。私は文字列にユーザーによって与えられた単語をスキャンし、これらの文字列へのポインタを配列char * wordsに保存する必要があります。その後、私はそれを暗号化し、次のようにそれらを印刷:cの暗号化機能

words[0][0-n]\n(「N」ワード=オリジナルしかしENCRと同じ長さの長さである)
words[1][0-k]\n(K「が」=語の長さでありますオリジナルと同じ長さですが、encr)

。 。 プログラミングへイム新しい、これらの文字列とのトラブルを抱えて...助け

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

#define KEY_SIZE 256 



char encrypt_char(unsigned char key[KEY_SIZE], char ch); 
int read_words(char* words[], int size, int max_str_len); 
void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]); 




    int main() 
{ 
    int i=0,j=0; 
    unsigned char key[KEY_SIZE] = { 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 
     'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'}; 

    char* words[1000];  //the words must bee a 2D array beacause its a string array . 
    int num_words=read_words(words,1000,100); 
    printf("number of words =%d",num_words);/////check 
    encrypt_words(words,num_words,key); 
    while (words[i][j]!=0) 
    { 
     printf("%c",words[i][j]); 
     i++,j++; 
    } 
    return 0; 
} 






char encrypt_char(unsigned char key[KEY_SIZE], char ch) 
{ 
    int temp=ch; 
    return key[temp]; 
} 





int read_words(char* words[], int size, int max_str_len) 
{ 
    printf("in the read words func\n"); ///check 
    int i=0,words_count=0; 
    char* temp=malloc((max_str_len+1)*sizeof(char)); 
    if (temp==NULL) 
    { 
     printf("Memory allocation failure\n"); 
     free(temp); 
     return 0; 
    } 
    printf("passed the fail test\n"); ////check 
    while (scanf("%s",temp)==1&&words_count<size) 
    { 
     printf("in the while \n"); 
     int length=strlen(temp); 
     char* the_word=malloc((length+1)*sizeof(char)); 
     strcpy(the_word, temp); 
     words[i]=(char*)the_word; 
     i++; 
     words_count++; 
     printf("the i=%d   words count=%d   words[%d]=%s \n",i,words_count,i,&words[i]);/////////check 
    } 
    printf("WORDS COUNT IS=%d\n",words_count); 
return words_count; 

} 


void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]) 
{ 
    int i=0,j=0; 
    while (words[i][j]!=0&&i<num_words) 
    { 
     char temp=words[i][j]; 
     words[i][j]=encrypt_char(key,temp); 
     i++,j++; 
    } 
} 

コンパイラmessege:あなたの問題は、このコードである

-------------- Build: Debug in hw4q2 (compiler: GNU GCC Compiler)--------------- 

mingw32-gcc.exe -Wall -Werror -pedantic -ansi -W -O -g -pedantic-errors -std=c99 -c "C:\Users\Anton\Desktop\C projects\hw4q2\hw4q2.c" -o obj\Debug\hw4q2.o 
mingw32-g++.exe -o hw4q2.exe obj\Debug\hw4q2.o 
Output file is hw4q2.exe with size 73.25 KB 
Process terminated with status 0 (0 minute(s), 0 second(s)) 
0 error(s), 0 warning(s) (0 minute(s), 0 second(s)) 
+0

:よう

何か。 –

+0

「これらの文字列に問題がある」より具体的に記述してください。特に問題は何ですか?問題のデバッグを試みたときに何を発見しましたか? –

+0

申し訳ありませんが、問題はそれがクラッシュすることです:(スキャンして、与えられた単語の数を表示しますが、暗号化にはならないと思いますが、私はcodeblocksを処理してコンパイルします.. –

答えて

0

:ここ

while (words[i][j]!=0) 
{ 
    printf("%c",words[i][j]); 
    i++,j++; 
} 

あなたは両方iをインクリメント同時にj。だから、次の文字と次の文字列の両方にジャンプします。あなたが文字列終了を越えて飛び越えれば、それは失敗します。

代わりにこれを試してみてください:

for (i=0; i<num_words; ++i) 
{ 
    printf("%s\n",words[i]); 
} 

同様の問題はここにある:あなたが(つまりのみjをインクリメント)最初の1つの文字列を終了し、次の文字列(すなわち増分に移動する必要が

void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]) 
{ 
    int i=0,j=0; 
    while (words[i][j]!=0&&i<num_words) 
    { 
     char temp=words[i][j]; 
     words[i][j]=encrypt_char(key,temp); 

     i++,j++; // Ups... 
    } 
} 

i)。あなたが持っている問題の種類を教えを忘れた

void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]) 
{ 
    int i=0,j=0; 
    for (i=0; i<num_words; ++i) 
    { 
     j = 0; 
     while (words[i][j]!=0) 
     { 
      char temp=words[i][j]; 
      words[i][j]=encrypt_char(key,temp); 
      j++; 
     } 
    } 
} 
+0

ありがとう、私は今それを試してみましょう! –

+0

@英語 - こちらをご覧くださいhttp://ideone.com/8OPkCa – 4386427

+0

ありがとうございます。もう一度申し訳ありませんが、もし私の質問が十分にはっきりしていないとあなたの助けに感謝! –