SASC pre-defined memxlt() and xltable() function

Discussion in 'C' started by selvamsrinivasan85, Nov 15, 2010.

  1. selvamsrinivasan85

    selvamsrinivasan85 New Member

    Joined:
    Nov 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello friends,

    I am in a need of creating a String function that has to replace SASC memxlt() and xltable() functions.

    Description:
    void *memxlt(void *blk, const char *table, size_t n);

    memxlt() - memxlt translates a block of memory from one character set to another. The first argument ( blk ) is the address of the area of memory to be translated, and the third argument ( n ) is the number of characters to be translated. The second argument ( table ) is a pointer to a 256-byte translate table, which should be defined so that table[c] for any character c is the value to which c should be translated. (The function xltable is frequently used to build such a table.)

    Note:
    The argument string is translated in place; that is, each character in the string is replaced by a translated character.

    Return value:
    memxlt returns a pointer to the translated string.

    Caution:
    The third argument to memxlt is size_t . If a negative number is passed, massive overlaying of memory occurs.
    The effect of memxlt is not defined if the source string and the translate table overlap.

    e.g:
    Code:
    #include <lcstring.h>
    #include <stdio.h>
    #include <stdlib.h>
    main(int argc, char *argv[])
    {
       int len, i, j;
       char a;
       char alphabet[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       char table[256];
       if (argc < 2) {
          puts("Specify the secret word on the command line.");
          exit(4);
       }
       len = strlen(argv[1] );
       memupr(argv[1], len);     /* Uppercase input message. */
          /* Randomize the alphabet.                         */
       for (i = 0; i < 26; i++)
          for (j = i + 1; j < 26; j++)
             if (rand() % 2) {
                a = alphabet[i];
                alphabet[i]  = alphabet[j] ;
                alphabet[j]  = a;
             }
          /* Build a translate table.                        */
       xltable(table,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",alphabet);
          /* Translate message.                              */
       memxlt(argv[1],table,len);
          /* Print message.                                  */
       printf("Today's secret word is: \n%s\n",argv[1]);
       return;
    }
    
    xltable() -
    char *xltable(char table[256], char *source, char *target);

    xltable builds a translation table that you can use later as an argument to the memxlt or strxlt function.
    The argument table is a 256-character array, in which the translation table is to be built. The second argument ( source ) is a string of characters that the table is to translate, and the third argument ( target ) is a string containing the characters to which the source characters are to be translated, in the same order. The source and target strings should contain the same number of characters; if they do not, the extra characters of the longer string are ignored.
    You can also specify a table address of 0. In this case, xltable builds the table in a static area and returns the address of this area. This area may be overlaid by the next call to xltable , strscntb , or memscntb .
    The table built by xltable translates any character not present in the source string to itself, so these characters are not changed when using the table.

    Please let me know if you need any more details.

    Thanks,
    S.Srinivasan,
    Mainframe Programmer Analyst.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The principle missing detail is why we should write this for you.
    Are you stuck somewhere? How far have you got writing this code?
    You are a Programmer Analyst according to you, maybe your employer expects YOU to write the code, not post the spec on forums and expect others to do it for you.
     
  3. selvamsrinivasan85

    selvamsrinivasan85 New Member

    Joined:
    Nov 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice