LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2005, 09:14 AM   #1
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Rep: Reputation: 15
want a c program to split (cut ) domain name from webaddess


hi
i need a c program or function to perform following operation

input string : "www.mail.yahoo.com"
output : " yahoo.com"
need string from second .(dot) from last

if input includes " / " after .com i neet to remove all the string after / including /

input : "www.mail.yahoo.com/india"
output : yahoo.com


need to combine both the functions in one

or give me idea which c fuction to use

Problem 2:


if ipaddress is given

input format : "192.54.89.90/india/myhome.htm"
output : 192.54.89.90


only ipaddress is needed


Its urgent, please need result immediatly
Thanks in advance :
 
Old 04-12-2005, 09:21 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Try looking into the functions split and explode, you could make something using those. Split the url adress at the . (dots), then checking which field has com or net or ... in it, and take that one as the .com one an then just glue the one infront of him to it. That way you should get www.yahoo.com => yahoo.com

You will probably have to modify this algorithm to work for your every need, but it is a guideline.
 
Old 04-12-2005, 09:51 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Try this:
Code:
itsme@itsme:~/C$ cat domainname.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char **argv)
{
  char *str, *p;
  int ctr = 0, alldigits = 1;

  if(argc != 2)
  {
    puts("Usage: domainname <url>");
    exit(1);
  }

  if((str = strstr(argv[1], "://")))
    str += 3;
  else
    str = argv[1];

  if((p = strchr(str, '/')))
    *p = '\0';
  else
    p = str + strlen(str);
  p--;

  while(p > str)
  {
    if(*p == '.')
    {
      if(ctr && !alldigits)
      {
        p++;
        break;
      }
      ctr++;
    }
    else
    {
      if(!isdigit(*p))
        alldigits = 0;
    }

    p--;
  }

  puts(p);
  return 0;
}
Code:
itsme@itsme:~/C$ ./domainname www.mail.yahoo.com
yahoo.com
itsme@itsme:~/C$ ./domainname www.mail.yahoo.com/india
yahoo.com
itsme@itsme:~/C$ ./domainname 192.54.89.90/india/myhome.htm
192.54.89.90
It will also work if you leave the protocol part of the url there too:
Code:
itsme@itsme:~/C$ ./domainname http://192.54.89.90/india/myhome.htm
192.54.89.90

Last edited by itsme86; 04-12-2005 at 09:53 AM.
 
Old 04-13-2005, 04:41 PM   #4
pld
Member
 
Registered: Jun 2003
Location: Southern US
Distribution: Ubuntu 5.10
Posts: 206

Rep: Reputation: 30
Was wondering if someone answered this homework question or not
 
Old 04-14-2005, 10:36 PM   #5
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
Thank you itsme86 .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what would make ever virtual domain name resolve to one domain name on my system kuplo Linux - Newbie 1 11-14-2005 06:57 PM
Cant get entry in bind to work with domain.com instead of www.domain.com pxes351 Linux - Networking 12 05-09-2005 06:20 AM
A linux program that can handle split .rar files? Cinematography Linux - Software 3 05-03-2005 03:04 PM
split and splitfields function cause an error when the program run Linh Programming 1 10-26-2003 03:10 PM
program to split and merge files fuelinux Linux - Software 3 10-25-2003 11:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration