C - 将大写字符转换为小写字符

时间:2016-09-13 17:55:56

标签: c arrays string pointers tolower

如何将这些字符转换为小写?使用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);

2 个答案:

答案 0 :(得分:3)

&#34; A&#34;是一个字符串。

&#39; A&#39;是一个角色。

因此,您正在为tolower()提供一个字符串,因此请更改:

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

到此:

fputc()

正如迪米特里所说,你想使用fputs(),而不是tolower()这对字符串有用..

正如Keith Thompson所说:如果参数为负且不等于EOF,则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

输出:

 <div class="form-group">
        @Html.LabelFor(model => model.Id_Componente, "Id_Componente", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input class="form-control" type="text" id="Id_Componente" name="Id_Componente" autofocus="autofocus" onkeypress="FunctionP(event)" />
            @Html.ValidationMessageFor(model => model.Id_Componente, "", new { @class = "text-danger" })
        </div>
    </div>

答案 1 :(得分:0)

您可以获取数组中的每个元素,将其放入辅助字符并使用 let tablesQuery = app.tables let cellQuery = tablesQuery.cells.containing(.staticText, identifier: "MyCell") let cell = cellQuery.children(matching: .staticText) let cellElement = cell.element cellElement.tap() let tableElement = tablesQuery.element tableElement.swipeUp() 然后putchar

tolower