当前位置:网站首页>1108. IP address invalidation

1108. IP address invalidation

2022-06-09 10:08:00 Mr Gao

1108. IP Address invalidation

Give you an effective IPv4 Address address, Go back to this IP Invalid version of address .

The so-called invalidation IP Address , In fact, it is to use “[.]” Instead of every “.”.

Example 1:

Input :address = “1.1.1.1”
Output :“1[.]1[.]1[.]1”

Example 2:

Input :address = “255.100.50.0”
Output :“255[.]100[.]50[.]0”

This question is also very simple , We set up a 3*strlen(address) Memory auxiliary string array , It can solve the problem well , Be careful , Finally, set the string array s[end]=‘\0’, This is very important .
The solution code is as follows :

char * defangIPaddr(char * address){
    
    int len=strlen(address);
    char *s =(char*)malloc(sizeof(char)*3*len);

    int i=0;
    int j=0;
    while(address[i]!='\0'){
    
        if(address[i]=='.'){
    
            s[j++]='[';
            s[j++]='.';
             s[j++]=']';
        }
        else{
    
            s[j++]=address[i];
        }

         i++;
    }
    s[j]='\0';
    return s;

}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090927456395.html