简体   繁体   中英

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.

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.

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

不,这不可能。

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 \\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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