C 函数不';t返回期望值,printf不';不返回任何值

C 函数不';t返回期望值,printf不';不返回任何值,c,string,debugging,encryption,printf-debugging,C,String,Debugging,Encryption,Printf Debugging,嗨,我是C新手,但不熟悉编程(只熟悉Python和JS等高级语言) 在我的CS任务中,我必须实现一个对加密字符串进行解码的函数。(使用的加密是atbash) 我想给decode函数一个编码字符串,然后接收一个解码字符串。 我通过打印出字符串的每个解码字符来测试我的函数,它成功了 但是,我在实现函数的原始任务时遇到问题(编码str->解码str) 这是我的密码: #include <stdio.h> #include <string.h> /***************

嗨,我是C新手,但不熟悉编程(只熟悉Python和JS等高级语言)

在我的CS任务中,我必须实现一个对加密字符串进行解码的函数。(使用的加密是atbash)

我想给decode函数一个编码字符串,然后接收一个解码字符串。 我通过打印出字符串的每个解码字符来测试我的函数,它成功了

但是,我在实现函数的原始任务时遇到问题(编码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;
}
#包括
#包括
/*******************/
//atbash解码函数
//接收atbash字符串并返回解码的字符串。
字符*解码(字符*str){
int i=0;
字符解码字符串[1000];
strcpy(解码字符串“\n”);
while(str[i]!='\0'){

如果(!((str[i]>=0&&str[i]<65)|(str[i]>90&&str[i]<97)|(str[i]>122&&str[i]='A'&&str[i]='A'&&str[i]=0&&str[i]<65)|(str[i]>90&&str[i]<97)|(str[i]>122&&str[i]问题:无法在函数的存储区内分配
char decoding[1000]。decodedString
中的
decoded
)将在对函数的调用结束时释放。您正在引用取消分配的内存。
strcat
的两个参数都应该是指向以null结尾的字符串的指针,而不是指向单个字符的指针。@dbush您能给我一个示例说明您的意思吗?谢谢大家的回答!我发现strcat放错了位置在我的代码中,我找到了一个简单的解决方案,一切都按计划进行。再次感谢您的耐心:)