3 Replies - 25610 Views - Last Post: 17 September 2006 - 06:49 PM Rate Topic: -----

#1 bigbang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-August 06

Extract substring from string

Posted 17 September 2006 - 10:21 AM

i have a small console program that shows you, your ip and resolves from your ip your host name(reverse dns) and display then your ip and host name
to the screen.
an example output:
IP: 12.34.567.89
Host Name: dslb-012-034-567-089.pools.myisp-ip.net


now i want to convert the
dslb-012-034-567-089.pools.myisp-ip.net 

to
dslb.pools.myisp-ip.net

or something similar.
my goal is to remove the numbers and the
-
character from the host name string, because my ip is a dynamic ip and it changes every
24hrs automatically. it should start to find the first
-
character and from there, until the first
.
character or last
number in the host name string and then cut out the found unneeded characters and then display the final host name string without the ip and
the
-
character.
i'm using msvc++ as compiler.

how would i do that?

any help and hints are more than welcome.

Is This A Good Question/Topic? 0
  • +

Replies To: Extract substring from string

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Extract substring from string

Posted 17 September 2006 - 12:37 PM

You can use the .find() and .substr() methods of the C++ string object to accomplish this...here is an overview on usage:

http://www.fredosaur...der-string.html
Was This Post Helpful? 0
  • +
  • -

#3 bigbang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-August 06

Re: Extract substring from string

Posted 17 September 2006 - 06:43 PM

View PostAmadeus, on 17 Sep, 2006 - 12:37 PM, said:

You can use the .find() and .substr() methods of the C++ string object to accomplish this...here is an overview on usage:

http://www.fredosaur...der-string.html

thanks f1shrman, i could solve it with help from a good online friend, his hint was to use:
#include <stdio.h>

void main()
{
char host[]="dslb-012-034-567-089.pools.myisp-ip.net";

printf("\n %s ", host);
strcpy(strchr(host,'-'),strchr(host,'.'));
printf("\n %s",host);

}


output:

dslb-012-034-567-089.pools.myisp-ip.net
dslb.pools.myisp-ip.net

and i modified it to:
	char* cShortHost = host->h_name;
	strcpy(strchr(cShortHost, '-'), strchr(cShortHost, '.'));

	printf("\nShortHost Name: %s", cShortHost);


alernative:
	printf("IP: %s", cIP);
	printf("\nHost Name: %s", host->h_name);

	WritePrivateProfileString("IP Info", "IP",   cIP,		  "c:\\HOST.ini");
	WritePrivateProfileString("IP Info", "HOST", host->h_name, "c:\\HOST.ini");

	strcpy(strchr(host->h_name, '-'), strchr(host->h_name, '.'));

	printf("\nShortHost Name: %s", host->h_name);

	WSACleanup();

	WritePrivateProfileString("IP Info", "SHORT HOST", host->h_name,   "c:\\HOST.ini");

This post has been edited by bigbang: 17 September 2006 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Extract substring from string

Posted 17 September 2006 - 06:49 PM

1) Use code tags to display code from next time
2) You need string.h header
3) and finally void mainer's are doomed :angry:

This post has been edited by Xing: 17 September 2006 - 06:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1