简体   繁体   English

用于识别大写/小写字母C的功能

[英]Function to identify upper/lower case letters C

I'm trying to create a function that will identify whether the first letter input is upper or lower case then output the rest of the string in that same case(upper/lower). 我正在尝试创建一个函数来识别第一个字母输入是大写还是小写,然后在相同的情况下(上/下)输出字符串的其余部分。

For example, "Hi there" would become "HI THERE" . 例如, "Hi there"将成为"HI THERE"

I'm not familiar with fgets . 我不熟悉fgets Once I run it I can input and press enter and the program doesn't run. 一旦我运行它,我可以输入并按回车,程序不运行。 I'm not getting any compiler errors. 我没有得到任何编译器错误。 I believe I went wrong in the void shift function. 我相信我在虚空移位功能上出错了。

Also, I know gets is not recommended, is fgets similar? 另外,我知道不推荐getsfgets类似吗? Or is it better to use scanf ? 或者使用scanf更好?

#include <stdio.h>
#include <ctype.h>
void shift (char *my_string); // Function declaration

int main()
{
  char inputstring[50];

  printf("Enter a string\n");
  char *my_string = inputstring;
  shift(my_string); // Function
}

void shift (char *my_string) // Function definition
{
  int i =0;
  char ch;

  for(i=0; i<50; i++)
    fgets(my_string, 50, stdin);

  while(my_string[i])
  {
    if(ch>='A' && ch<= 'Z') // When first char is uppercase
    {
      putchar (toupper(my_string[i]));
      i++;
    }
    else if (ch>='a' && ch <= 'z') // When first char is lowercase
    {
      putchar(tolower(my_string[i]));
      i++
    }
  }
  return;
}

You don't need to call fgets() fifty times. 你不需要五十次调用fgets() It reads a line from stdin and writes it to my_string . 它从stdin读取一行并将其写入my_string It seems you only want to read one line, not fifty (and keep only the last one). 看起来你只想读一行,而不是五十(而只保留最后一行)。 The 50 is the maximum number of characters (minus one) that will be read and written to the buffer. 50是将被读取和写入缓冲区的最大字符数(减1)。 This limit is to prevent buffer overflow. 此限制是为了防止缓冲区溢出。 See fgets() . fgets()

Try removing the for loop on the line before the fgets() call. 尝试在fgets()调用之前删除该行上的for循环。 Also, you don't need the my_string in main() . 此外,您不需要main()my_string The corrected code: 更正后的代码:

#include <stdio.h>
#include <ctype.h>
void shift (char *my_string);//function declaration

int main()
{
  char inputstring[50];

  printf("Enter a string\n");
  shift(inputstring);
}

void shift (char *my_string) //function definition
{
  int i;
  char ch;

  if ( fgets(my_string, 50, stdin) == NULL )
    return;

  ch = my_string[0];
  for ( i=0; my_string[i]; i++ )
  {
    if(ch>='A' && ch<= 'Z') //when first char is uppercase
    {
        putchar (toupper(my_string[i]));
    }
    else if (ch>='a' && ch <= 'z')//when first char is lowercase
    {
        putchar(tolower(my_string[i]));
    }
  }
  return;
}

Edit: Added ch initialization, pointed out by @thurizas. 编辑:添加了ch初始化,由@thurizas指出。 Changed while loop to for loop. 改变while循环来for循环。 Added check to return value of fgets() as suggested by @JonathanLeffler. 添加了检查以返回@JonathanLeffler建议的fgets()值。 (See his comment about the buffer size.) (参见他对缓冲区大小的评论。)

  • Here is another solution for your problem, 这是您的问题的另一种解决方案,

     #include <stdio.h> #include <ctype.h> void convertTo (char *string); int main() { char inputString[50]; printf("Enter a string\\n"); convertTo(inputString); } void convertTo (char *string) { int i; char ch; gets(string); ch = string[0]; for ( i=0; string[i]; i++ ) { if(ch>='A' && ch<= 'Z') { if(string[i]>='a' && string[i]<= 'z') string[i] = string[i] - 32; } else if (ch>='a' && ch <= 'z') { if(string[i]>='A' && string[i]<= 'Z') string[i] = string[i] + 32; } } printf("%s\\n", string); return; } 

All ASCII characters are represented by 7-bits. 所有ASCII字符均由7位表示。 (thus the term 7-bit ASCII) The only bitwise difference between lower-case and upper-case is that bit-5 (the sixth bit) is set for lowercase and cleared (unset) for uppercase. (因此术语为7位ASCII) 小写大写之间的唯一按位差异是bit-5(第六位)设置为小写并清除(unset)为大写。 This allows a simple bitwise conversion between lowercase and uppercase (either by adding/subtracting 32 or by simply flipping bit-5 directly.) 这允许在小写和大写之间进行简单的按位转换(通过添加/减去32或直接简单地翻转位5)。

      +-- lowercase bit
      |
a = 01100001    A = 01000001
b = 01100010    B = 01000010
c = 01100011    C = 01000011
...

This allows a simple test and conversion if the first character is upper-case: 如果第一个字符是大写的话,这允许简单的测试和转换:

#include <stdio.h>

enum { MAXC = 50 };

char *shift (char *my_string);

int main (void)
{
    char inputstring[MAXC] = {0};;

    printf ("\n Enter a string: ");
    if (shift (inputstring))
        printf (" my_string is  : %s\n", inputstring);

    return 0;
}

char *shift (char *my_string)
{
    char *p;

    if (!(p = fgets (my_string, MAXC, stdin))) return NULL;

    if (*p == '\n') return NULL;   /* Enter only pressed  */

    if ('A' <= *p && *p <= 'Z')    /* test for upper case */
        for (; *p; p++)            /* convert lower/upper */
            if ('a' <= *p && *p <= 'z') *p &= ~(1 << 5);

    return my_string;
}

Example Use 示例使用

$ ./bin/case_1st_to_upper

 Enter a string: this is my string
 my_string is  : this is my string

$ ./bin/case_1st_to_upper

 Enter a string: This is my string
 my_string is  : THIS IS MY STRING

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

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