Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a Certain character

Status
Not open for further replies.

lisal

Programmer
Sep 22, 2005
5
US
What command can I use to do the following...

I create a string City; and I want to search for the character "," and if there is a "," in it it will place that string in the variable city.

i.e. Newport News, VA It will take the Newport News, and put it under city etc...Thanx :)


Lisa
 
if it's a char array you could do something like:

char* p;
int i = 0;
for(p=City; *p!=0; p++, i++)
[tab]if(*p == ',') break;

if(*p == ',') // if comma was found
{
[tab]city = new char[i + 1];
[tab]strncpy(city, City, i);
[tab]city[ i ] = 0;
}

--Will Duty
wduty@radicalfringe.com

 
I believe you can use strtok (your_string,",");

char* tok;

tok = strtok(my_string,",");

if(tok)// you have a city (comma existed)

Matt
 
even better! I've never used strtok.
--Will Duty
wduty@radicalfringe.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top