-2

please can you give me some ideas to convert a string such as: char string[20]="www.msn.es" to another string : 3www3msn2es0 . I just want ideas please, should I use strstr or you just can do it with char pointer and bucles. Thanks so much, it's my first time in this forum.

  • I would recommend rephrasing your question. It is ambiguous as to what you are after. Also, you should at least tell us what you've tried so far if you want someone to answer. – nonsensickle Aug 01 '13 at 00:52
  • 3
    Welcome to StackOverflow. This web site is more suited for questions about *specific* problems that you are having, not general solutions. Have a crack at solving the problem yourself, and if you get stuck, let us know what's troubling you. – dreamlax Aug 01 '13 at 00:54

1 Answers1

1

This works for me (note that it assumes the length of each segment in the hostname will be 9 chars or less, though):

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

int main(int argc, char ** argv)
{
   if (argc < 2)
   {
      printf("Usage:  ./rr www.msn.es\n");
      return 10;
   }

   char outbuf[256] = "\0";
   const char * in = argv[1];
   char * out = outbuf;
   while(*in)
   {
      const char * nextDot = strchr(in, '.');
      if (nextDot == NULL) nextDot = strchr(in, '\0');
      *out++ = (nextDot-in)+'0';
      strncpy(out, in, nextDot-in);
      out += (nextDot-in);
      if (*nextDot == '\0') break;
                       else in = nextDot+1;
   }
   *out++ = '0';
   *out++ = '\0';
   printf("%s\n", outbuf);

   return 0;
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234