'How to create new escape sequence?

Is it possible to make a whole printf() string be converted toupper() or tolower() automatically via an escape sequence? How to do that?

Escape sequence are those that having "\" back slash when printing. tolower() enables you to convert uppercase letter to lowercase. toupper() enables you to convert lowercase letter into uppercase. But is it possible to make/add an escape sequence that also have the same function of tolower() and toupper(). For example, I'm going to create new escape sequence "\u" and "\l". \u as the toupper() and it will be used like this.

printf("\u hello world");

Output:

HELLO WORLD

The string next to the escape sequence will be converted to uppercase letters. It is also the same with the \l as tolower(). How to do that? Please help me.



Solution 1:[1]

Is it possible to make a whole printf() string be converted toupper() or tolower() automatically via an escape sequence?

No, you cannot do that.

The escape sequences are defined by the language. You can't add to them.

Solution 2:[2]

  • You can make new function as Printf_new() and in that you can parse
  • its argument and get \i or \u and depending upon that
  • call toupper() and tolower() convert your string and then again
  • call original printf()

Solution 3:[3]

No. This isn't in any way possible.

Solution 4:[4]

You could create your own print function with your own inbuilt escape sequences. Something like this:

printf_reimagined(const char * format, ... )
{
  const char * formatted;
  for(int i = 0; format[i] != '\0'; i++)
  {
    if(format[i] == '//'
    {
      switch(format[i+])
      {
        case 'u':
          // Convert to upper case
          ... do formatting
          break;
        case 'l':
          // Convert to lower case
          ... do formatting
          break;
      }
    }
  }
  
  printf(formatted, ...);
}

You would then use \\u or \\l for your escape sequences. Sorting out any other arguments would need to be done here too mind.

I did something similar recently but I did have to make \ become \\

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 R Sahu
Solution 2 Jeegar Patel
Solution 3
Solution 4 Electro_BRedding