0

こんにちは、私は C は初めてですが、プログラミングは初めてです (Python や JS などの高水準言語の経験しかありません)。

CS の割り当てでは、暗号化された文字列をデコードする関数を実装する必要があります (使用される暗号化は atbash です)。

デコード関数にエンコードされた文字列を与え、デコードされた文字列を受け取りたいです。文字列のデコードされたすべての文字を出力して機能をテストしたところ、機能しました。

ただし、関数の元のタスクの実装に問題があります (エンコードされた str -> デコードされた str)

これが私のコードです:

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

/*******************/
// atbash decoding function
// recieves an atbash string and returns a decoded string.
char *decode(char *str){

  int i = 0;
  char decodedString[1000];
  strcpy(decodedString, "\n");

  while(str[i] != '\0') {

    if(!((str[i] >= 0 && str[i] < 65)||(str[i] > 90 && str[i] < 97)||(str[i] > 122 && str[i] <=127))){

        if(str[i] >= 'A' && str[i] <= 'Z'){
          char upperCaseLetter = 'Z'+'A'-str[i];
          strcat(decodedString, &upperCaseLetter);
        }

        if(str[i] >= 'a' && str[i] <= 'z'){
          char lowerCaseLetter = 'z'+'a'-str[i];
          strcat(decodedString, &lowerCaseLetter);
        }
      }

    if(((str[i] >= 0&& str[i] < 65)||(str[i] > 90 && str[i] < 97)||(str[i] > 122 && str[i] <= 127))){
      char notALetter = str[i];
      strcat(decodedString, &notALetter);
    }
   i++;
  }
  printf("%s\n", decodedString); // Debug: Checking what I would receive as a return, expected "Hello World!", got binaries
  return decodedString;
}

int main(){


  char *message = "Svool Dliow!";

  printf("This is the decode String:\n%s",(decode(message))); //Expected return of "This is the decode String:\nHello World!", received "This is the decode String:\n" instead



  return 0;
}

問題:

(1)

文字列 ("Hello World!") ではなく、いくつかのバイナリをデバッグ コメントで受け取ります。

(2)

printf("\n%s", (decoded(message))); の理由がわかりません。関数decode ._のコールバックを出力しません。

前もって感謝します!

編集:

問題 (2) は paulsm4 のおかげで解決されました

編集2:

問題 (1) は dbush のおかげで解決されました。

4

0 に答える 0