小写< - >大写函数不按计划工作

Bra*_*cus 5 c bitmask

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

这是我正在使用的:

#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)

在学习的过程中.谢谢你的时间.

her*_*tao 2

  1. 您忘记将空终止添加到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)
  2. 您需要更改while(*ptrword != NULL)while(*ptrword != '\0').

  3. 正如@ooga 指出的,您最好为flipped. 所以char flipped[] = "";改成char flipped[100] = "";.

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