所以,我要做的是创建一个将大写字符切换为小写的函数,反之亦然.
这是我正在使用的:
#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;
}
Run Code Online (Sandbox Code Playgroud)
在学习的过程中.谢谢你的时间.
您忘记将空终止添加到switched
. 你需要添加
switched[counter] = '\0'; // add '\0' to the end
Run Code Online (Sandbox Code Playgroud)
前
printf("Your flipped text is: %s \n", switched);
Run Code Online (Sandbox Code Playgroud)您需要更改while(*ptrword != NULL)
为while(*ptrword != '\0')
.
正如@ooga 指出的,您最好为flipped
. 所以char flipped[] = "";
改成char flipped[100] = "";
.
解决这些问题后,它应该按预期工作。在Ideone上查看运行结果。