简体   繁体   English

切换字符大小写,指针问题

[英]switching char cases, pointer problems

The goal of my program is to switch the cases of the two letters inputted. 我程序的目标是切换输入的两个字母的大小写。 For example a turns to A and B turns to b. 例如,a转向A,B转向b。 I am having trouble getting my code to work when I know my logic is correct. 当我知道自己的逻辑正确时,我很难使我的代码正常工作。

#include <stdio.h>;

void switcharoo(char *ch1,char *ch2)
{

    if(*ch1 >= 'a' && *ch1 <= 'z' && *ch2 >= 'A' && *ch2 <= 'Z')
    {
        *ch1 = 'A' + *ch1 - 'a';
        *ch2 = 'a' + *ch2 - 'A';
    }
    else if(*ch1 >= 'A' && *ch1 <= 'Z' && *ch2 >= 'A' && *ch2 <= 'Z')
    {
        *ch1 = 'a' + *ch1 - 'A';
        *ch2 = 'a' + *ch2 - 'A';
    }
    else if(*ch1 >= 'a' && *ch1 <= 'z' && *ch2 >= 'a' && *ch2 <= 'z')
    {
        *ch1 = 'A' + *ch1 - 'a';
        *ch2 = 'A' + *ch2 - 'a';
    }
    else if(*ch1 >= 'A' && *ch1 <= 'Z' && *ch2 >= 'a' && *ch2 <= 'z')
    {
        *ch1 = 'a' + *ch1 - 'A';
        *ch2 = 'A' + *ch2 - 'a';
    }
}

int main()
{
    char lettera,letterb;

    printf("Please input a first character: ");
    scanf("%c", &lettera);
    printf("Please input a first character: ");
    scanf("%c", &letterb);

    switcharoo(lettera,letterb);
    printf("%c\n %c\n",lettera,letterb);

    return 0;
}
scanf("%c", &lettera);
scanf("%c", &letterb);

This probably isn't doing what you want. 这可能不是您想要的。 The newline (enter) character you presumably type after entering the first letter (given your prompts imply you're expecting them on different lines) will be read in as the second letter. 您大概在输入第一个字母后输入的换行符(输入)(假设您的提示暗示您期望它们在不同的行上)将作为第二个字母读入。 You'd have to type two letters then a newline for them to be read into your lettera and letterb successfully... can just prompt once for 2 characters. 您必须输入两个字母,然后输入一个换行符,才能将它们成功读入您的letteraletterb ...只需输入一次两个字符即可。 Also, it's good practice to check the return value of scanf to ensure it got the input you requested.... 另外,优良作法是检查scanf的返回值以确保它获得了您要求的输入。

Unless I am required to create my own function, I'd use #include <ctype.h> functions to make my life easier. 除非需要创建自己的函数,否则我将使用#include <ctype.h>函数使我的生活更轻松。

#include <ctype.h>
#include <stdio.h>

int main()
{
    char lettera,letterb;

    printf("Please input a first character: ");
    scanf("%c", &lettera);
    printf("Please input a first character: ");
    scanf(" %c", &letterb); // note the space in front of %c to flush '\n'

    // lettera conversion
    if(isupper(lettera)) {
        lettera = tolower(lettera);
    } else if (islower(lettera)) {
        lettera = toupper(lettera);
    }

    // letterb conversion
    if(isupper(letterb)) {
        letterb = tolower(letterb);
    } else if (islower(letterb)) {
        letterb = toupper(letterb);
    }

    printf("%c\n %c\n",lettera,letterb);

    return 0;
}

You should call 你应该打电话

switcharoo(lettera,letterb);

as

switcharoo(&lettera,&letterb); //passing address of lettera and letterb

as you are taking char * in the function. 当您在函数中使用char *时。

Your code will not run. 您的代码将无法运行。 The compiler will give error as con not convert char to char *. 编译器会给出错误,因为con不能将char转换为char *。

Try calling your function the way Rohan has suggested. 尝试按照Rohan建议的方式调用函数。

Or you can simplify your function. 或者,您可以简化功能。 Use it for one parameter only and call it twice. 仅将其用于一个参数,然后调用两次。

char changeCase(char ch) {
  if (ch >= 'A' && ch <= 'Z')
    ch -= 32;
  if (ch >= 'a' && ch <= 'z')
    ch += 32;
  return ch;
}

In main you can write, 基本上你可以写

char lettera, letterb;
//get values of lettera and letterb
lettera = changeCase(lettera);
letterb = changeCase(letterb);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM