【问题标题】:C - converting a uppercase char to lowercase charC - 将大写字符转换为小写字符
【发布时间】:2016-09-13 17:55:56
【问题描述】:

如何将这些字符转换为小写?使用 tolower() 不起作用。

我有一个这样的数组:

static char clef[][7] =
{
  ['A'] = "X",
  ['B'] = "Y",
  ['C'] = "Z",
  ['D'] = "A",
  ['E'] = "B",
  ['F'] = "C",
  ['G'] = "D",
  ['H'] = "E",
  ['I'] = "F",
  ['J'] = "G",
  ['K'] = "H",
  ['L'] = "I",
  ['M'] = "J",
  ['N'] = "K",
  ['O'] = "L",
  ['P'] = "M",
  ['Q'] = "N",
  ['R'] = "O",
  ['S'] = "P",
  ['T'] = "Q",
  ['U'] = "R",
  ['V'] = "S",
  ['W'] = "T",
  ['X'] = "U",
  ['Y'] = "V",
  ['Z'] = "W"

};

此代码旨在根据上述键数组中的移位替换文本中的字母。新文本全部大写。我想让它们小写,除非在“。”之后的情况下。标记新句子的开头。

  static void crack(FILE *fp, const char *buffer, const char *pad1, const char *pad2, int shift_index)
  {
    int c;
    char d;
    const char *pad = pad1;
    int col = 0;

    idx = shift_index - 4;

    for (int i = 0; (c = buffer[i]) != '\0'; i++)
    {
      if (col == 0)
      {
        fputs(pad, fp);
        col += strlen(pad);
        pad = pad2;
      }

      col++;
      c = toupper(c);

      printf("C :: %d", c);

      if (c < MAX_CLEF && clef[c][0] != '\0')
      {

        /*fputs(clef[c - idx], fp);
        printf("Value : %s", clef[c-idx]);*/


        if (buffer[i - 1] == '.') {
          fputs(clef[c - idx], fp);
        }
        else {
          fputs(tolower(clef[c-idx]), fp);
        }

        col += strlen(clef[c - idx]);
      }
      else
      {
        putc(c, fp);
        col++;

        printf("C :: right here %d", c);
      }
      if (col > 72)
      {
        putc('\n', fp);
        col = 0;
      }


    }

  }

我在编译时收到了一些警告:

incompatible pointer to integer conversion passing 'char [7]' to parameter
      of type 'int' [-Wint-conversion]
          fputs(tolower(clef[c-idx]), fp);

incompatible integer to pointer conversion passing 'int' to parameter of
      type 'const char *' [-Wint-conversion]
          fputs(tolower(clef[c-idx]), fp);

【问题讨论】:

  • fputs(tolower(clef[c-idx]), fp);--> fputc(tolower(*clef[c-idx]), fp);
  • @BLUEPIXY 所说的也可以,如我的回答所示。

标签: c arrays string pointers tolower


【解决方案1】:

“A”是一个字符串。

'A' 是一个字符。

因此你用字符串喂tolower(),所以改变这个:

fputs(tolower(clef[c-idx]), fp);

到这里:

fputc(tolower(clef[c-idx][0]), fp);

正如 Dimitri 所说,你想使用 fputc(),而不是 fputs(),这对字符串很有用..

正如 Keith Thompson 所说:如果参数为负数且不等于 EOF,tolower() 的行为未定义。要转换 char 参数,您需要将其转换为 unsigned char

小例子:

#include <stdio.h>
#include <ctype.h>
int main (void)
{
    char clef[][2] =
    {
        ['A'] = "X",
    };

    printf("Uppercase = %s\n", clef['A']);
    printf("Lowercase = %c\n", tolower((unsigned char)clef['A'][0]));

    // or equivalently, as BLUEPIXY stated
    printf("Lowercase = %c\n", tolower((unsigned char)(*clef['A'])));    

    return 0;
}

输出:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out 
Uppercase = X
Lowercase = x
Lowercase = x

【讨论】:

  • 您是否建议在调用 tolower() 之前将字符串转换为字符?用“A”声明数组并没有解决问题。
  • @user25976 你可以试试clef[index][0]
  • 谢谢@Dmitri,错过了:)
  • tolower() 如果参数为负数且不等于 EOF,则具有未定义的行为。要转换@​​987654334@ 参数,您需要将其转换为unsigned chartolower((unsigned char)clef[c-idx])
  • @user25976 我用一个最小的例子更新了我的答案,总结了所有信息。基思,谢谢你的评论! ;)
【解决方案2】:

您可以获取数组中的每个元素,将其放入辅助字符中并使用putchar,然后使用tolower

#include <stdio.h>

int main(void) {
    int counter=0;
    char myChar;

    char str[]="ABCDEFG.\n";

    while (str[counter])
    {
        myChar=str[counter];
        putchar (tolower((unsigned char)myChar));
        counter++;
    }
    return 0;
} 

【讨论】:

  • 重复我对另一个答案的评论:如果参数为负数且不等于 EOFtolower() 的行为未定义。要转换@​​987654326@ 参数,您需要将其转换为unsigned chartolower((unsigned char)clef[c-idx])
猜你喜欢
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多