0

How can I convert these characters to lowercase? Using tolower() is not working.

I have an array like this:

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"

};

This code is intended to replace letters in a text based on a shift in the key array above. The new text is all in uppercase. I would like to make them lower case except in cases that follow the '.' marking the beginning of a new sentence.

  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;
      }


    }

  }

I'm getting some warnings when compiling however:

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

and

incompatible integer to pointer conversion passing 'int' to parameter of
      type 'const char *' [-Wint-conversion]
          fputs(tolower(clef[c-idx]), fp);
2
  • 2
    fputs(tolower(clef[c-idx]), fp);--> fputc(tolower(*clef[c-idx]), fp); Commented Sep 13, 2016 at 18:10
  • What @BLUEPIXY said will work too, as shown in my answer. Commented Sep 13, 2016 at 18:24

2 Answers 2

3

"A" is a string.

'A' is a character.

Thus you are feeding tolower() with a string, so change this:

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

to this:

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

As Dimitri said, you want to use fputc(), not fputs() which is good for strings..

As Keith Thompson stated: tolower() has undefined behavior if the argument is negative and not equal to EOF. To convert a char argument, you need to convert it to unsigned char.

Minimal example:

#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;
}

Output:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out 
Uppercase = X
Lowercase = x
Lowercase = x
Sign up to request clarification or add additional context in comments.

5 Comments

are you suggesting to convert the string to a character before calling tolower()? declaring the array with 'A' hasn't solved the issue.
@user25976 you might try clef[index][0]
Thank you @Dmitri, missed that :)
tolower() has undefined behavior if the argument is negative and not equal to EOF. To convert a char argument, you need to convert it to unsigned char: tolower((unsigned char)clef[c-idx])
@user25976 I updated my answer with a minimal example, summarizing all the information. Keith, thanks for your comment! ;)
0

You can get every element in the array, put it in an aux char and use putchar and then 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;
} 

1 Comment

Repeating my comment on the other answer: tolower() has undefined behavior if the argument is negative and not equal to EOF. To convert a char argument, you need to convert it to unsigned char: tolower((unsigned char)clef[c-idx])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.