Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   SASC pre-defined memxlt() and xltable() function (http://www.go4expert.com/forums/sasc-pre-defined-memxlt-xltable-function-t23879/)

selvamsrinivasan85 15Nov2010 22:16

SASC pre-defined memxlt() and xltable() function
 
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.

xpi0t0s 16Nov2010 12:04

Re: SASC pre-defined memxlt() and xltable() function
 
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.

selvamsrinivasan85 16Nov2010 16:45

Re: SASC pre-defined memxlt() and xltable() function
 
Quote:

Originally Posted by xpi0t0s (Post 74758)
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.

Thanks for the reply.


All times are GMT +5.5. The time now is 05:33.