C 小写字母<--&燃气轮机;大写函数未按计划工作
所以,我要做的是创建一个函数,将大写字符转换为小写字符,反之亦然 以下是我的工作内容:C 小写字母<--&燃气轮机;大写函数未按计划工作,c,bitmask,C,Bitmask,所以,我要做的是创建一个函数,将大写字符转换为小写字符,反之亦然 以下是我的工作内容: #include <stdio.h> #include <stdlib.h> int caplowswitch(char string[], char switched[]); int main(){ char name[] = "whyisthisnotworking"; char flipped[] = ""; caplowswitch(name,
#include <stdio.h>
#include <stdlib.h>
int caplowswitch(char string[], char switched[]);
int main(){
char name[] = "whyisthisnotworking";
char flipped[] = "";
caplowswitch(name, flipped);
return 0;
}
int caplowswitch(char word[], char switched[]){
char *ptrword = word;
unsigned short int counter = 0;
printf("Your text input is: %s \n", word);
while(*ptrword != NULL){
switched[counter] = (*ptrword ^ 0x20);
counter++;
ptrword++;
}
printf("Your flipped text is: %s \n", switched);
return 1;
}
#包括
#包括
int-caplowswitch(字符字符串[],字符切换[]);
int main(){
char name[]=“为什么这不起作用”;
字符翻转[]=“”;
卡普劳开关(名称,翻转);
返回0;
}
int-caplowswitch(字符字[],字符交换[]){
char*ptrword=word;
无符号短整型计数器=0;
printf(“您的文本输入为:%s\n”,word);
while(*ptrword!=NULL){
切换[计数器]=(*ptrword^0x20);
计数器++;
ptrword++;
}
printf(“您翻转的文本为:%s\n”,已切换);
返回1;
}
在学习的过程中。谢谢您的时间。您没有给翻转的
足够的空间。通过将其定义和初始化为:
char flipped[] = "";
您只给它一个字符,初始化为'\0'
,因为这种形式的定义只分配足够的空间来容纳给定的字符串,而您传递的是空字符串。
试一试
您忘记将空终止添加到切换的
。您需要添加
switched[counter] = '\0'; // add '\0' to the end
以前
printf("Your flipped text is: %s \n", switched);
您需要将while(*ptrword!=NULL)
更改为while(*ptrword!='\0')
正如@ooga所指出的,您最好为翻转的分配足够的空间。所以更改字符翻转[]=”代码>到字符翻转[100]=“”代码>
在解决了这些问题之后,它应该能按预期工作。查看上的运行结果。代码中有三个错误
第1点:
从不字符翻转[]=”代码>像这样分配内存。这不是一个正确的程序
第2点:
当(*ptrword!=null)
时,不要像这样检查空字符。您应该像(*ptrword!='\0')一样检查此选项
第3点:
切换的
需要空终止。所以在while(*ptrword!=NULL){…}之后,设置开关[counter]='\0'
,这不起作用。。。怎样?我们应该猜猜问题出在哪里吗?我们是来帮助你的,而不是你的调试器。你输入的字符到哪里去了?(提示:数组中没有切换/翻转,因为只有一个字符长。)谢谢。我真不敢相信我错过了这一点。别忘了其他要点,比如没有为翻转的分配足够的内存,以及使用NULL
的错误,你的意思是'\0'
。
printf("Your flipped text is: %s \n", switched);