How to convert hostname to DNS name?

335 views Asked by At

I am trying to make program that converts hostname to DNS name.

So if I have www.google.com I want to convert it to 3www6google3com0

I tried with this code but it doesn't work . Can anyone tell me what I am doing wrong?

int main()
{
 unsigned char *a,niz[65536];
unsigned char host[]="www.google.ba";
a=(unsigned char*)&niz[12];
int lock = 0 , i;
    strcat((char*)host,".");

    for(i = 0 ; i < strlen((char*)host) ; i++) 
    {
        if(host[i]=='.') 
        {
            *a++ = i-lock;
            for(;lock<i;lock++) 
            {
                *a++=host[lock];
            }
            lock++; 
        }
    }
    *a++='\0';
printf("%s\n",a);
return 0;

When I try to print it in terminal is shows me blank space.

1

There are 1 answers

8
Steephen On BEST ANSWER

Instead of unsigned char using char. Using tools like strtok to tokenize the original string. sprintf to convert int to c-type string.

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
   char *a,niz[65536];
   char host[]="www.google.ba";
   a=( char*)&niz[20];

   strcat(a," ");
   char * token = strtok(host,".");
   char  buffer[20];
   while( token != NULL)
   {
       int len= strlen(token);
       sprintf(buffer,"%d",len);
       strcat(a,token);
       strcat(a,buffer);

       token=strtok(NULL,".");
   }
    *a++='\0';
    printf("%s\n",a);
    return 0;
}

O/P www3google6ba2

Edit:

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
   char *a,niz[65536];
   char host[]="www.google.ba";
   a=( char*)&niz[21];

   strcat(a," ");
   char * token = strtok(host,".");
   char  buffer[20];
   while( token != NULL)
   {
       int len= strlen(token);
       sprintf(buffer,"%d",len);
       strcat(a,token);
       strcat(a,buffer);

       token=strtok(NULL,".");
   }

    *a++='\0';
    int last_len=strlen(a);
     a[last_len-1]='0';
    printf("%s\n",a);
    return 0;
}

O/P www3google6ba0

Edit:3 It is a hint to resolve your issue:

#include<stdio.h>
int main()
{

  char c='0';
  printf("%d\n",c);

  c='3';
  printf("%d\n",c);

  c='3';
  printf("%d\n",c-'0');

}