Вопрос

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.

Это было полезно?

Решение

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top