gpt4 book ai didi

c - 小写<-->大写函数未按计划运行

转载 作者:太空狗 更新时间:2023-10-29 15:04:09 27 4
gpt4 key购买 nike

所以,我想做的是创建一个将大写字符切换为小写字符的函数,反之亦然。

这是我正在使用的:

#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;
}

在学习的过程中。感谢您的宝贵时间。

最佳答案

  1. 您忘记为 switched 添加空终止符。你需要添加

    switched[counter] = '\0';  // add '\0' to the end

    之前

    printf("Your flipped text is: %s \n", switched);
  2. 您需要将 while(*ptrword != NULL) 更改为 while(*ptrword != '\0')

  3. 正如@ooga 所指出的,您最好为flipped 分配足够的空间。因此,将 char flipped[] = ""; 更改为 char flipped[100] = "";

解决这些问题后,它应该会按预期工作。在Ideone上查看运行结果.

关于c - 小写<-->大写函数未按计划运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21667466/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com