C 这里的代码有什么问题,是为了找到凯瑟斯密码?
我试图将char pointer type作为函数的返回类型传递,但没有得到任何输出。代码如下:C 这里的代码有什么问题,是为了找到凯瑟斯密码?,c,char,return,C,Char,Return,我试图将char pointer type作为函数的返回类型传递,但没有得到任何输出。代码如下: #include<stdio.h> #include<string.h> char * decrypt(char* pt) { char* result=malloc(20); while(*pt) { *result=*pt+3;//incrementing by 3 alphbets and copying in reslult
#include<stdio.h>
#include<string.h>
char * decrypt(char* pt)
{
char* result=malloc(20);
while(*pt)
{
*result=*pt+3;//incrementing by 3 alphbets and copying in reslult
pt++;
result++;
}
*result='\0';
return result;
}
int main()
{
char plaintext[20];
scanf("%s",plaintext);//getting input
char *ct= decrypt(plaintext); //passing to function
printf("\nCiphertext %s",ct);//printing reslut
}
#包括
#包括
char*解密(char*pt)
{
char*result=malloc(20);
while(*pt)
{
*结果=*pt+3;//增加3个alphbets并在reslult中复制
pt++;
结果++;
}
*结果='\0';
返回结果;
}
int main()
{
字符纯文本[20];
scanf(“%s”,纯文本);//获取输入
char*ct=decrypt(明文);//传递给函数
printf(“\nCiphertext%s”,ct);//正在打印reslut
}
结果将在循环后指向\0
只需添加临时指针指向字符串的开头并返回
char * decrypt(char* pt)
{
char* result=malloc(20);
char *start = result;
while(*pt)
{
*result=*pt+3;//incrementing by 3 alphbets and copying in reslult
pt++;
result++;
}
*result='\0';
return start;
}
result
将在循环后指向\0
只需添加临时指针指向字符串的开头并返回
char * decrypt(char* pt)
{
char* result=malloc(20);
char *start = result;
while(*pt)
{
*result=*pt+3;//incrementing by 3 alphbets and copying in reslult
pt++;
result++;
}
*result='\0';
return start;
}
在printf的末尾添加“\n”。另外,您正在泄漏内存。如果malloc
result
将指向字符串的结尾,请不要忘记free
。检查malloc()的结果,它可能为空。在printf的结尾添加一个“\n”。另外,您正在泄漏内存。如果malloc
result
将指向字符串的结尾,请不要忘记free
。检查malloc()的结果,它可能为空