Search a string of ','

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 2
Reputation: Valadair is an unknown quantity at this point 
Solved Threads: 0
Valadair Valadair is offline Offline
Newbie Poster

Search a string for ','

 
0
  #1
Mar 9th, 2007
Title should read "search a string for ',' "

Afternoon folks. I visit this site a bit, especially when searching google for help on a particular problem. This is my first post, so let me get right to the point.

I won't go into the gory details of the program, only what I think is relevant. I'm working with date strings which will be read into the program in two formats:

"November 29, 2005"

or

"11 29 2005"


What I am attempting to do, but can't figure out, is set up an if/else control. Here is the psuedocode of what I'm trying:

  1. if (string contains a ',')
  2. //do this
  3.  
  4. else //string contains no ','
  5. //do this
So basically, I need to search the string for a comma, and if it finds one, process it with the following, else, do something else.

I know this isn't highly technical. I figured psuedocode would accomplish what I'm seeking.

Thanks in advance!

Val
Last edited by Valadair; Mar 9th, 2007 at 6:53 pm. Reason: Title should read "Search a string for ',' "
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,588
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Search a string of ','

 
0
  #2
Mar 9th, 2007
>I figured psuedocode would accomplish what I'm seeking.
You didn't specify if this is to be in C or C++. In C, look for the strchr function. In C++, look for the find_first_of member function of the string type.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 12
Reputation: snedan is an unknown quantity at this point 
Solved Threads: 0
snedan snedan is offline Offline
Newbie Poster

Re: Search a string of ','

 
0
  #3
Mar 12th, 2007
im not sure what are you looking for, but thats a function that checks string for a char and returns true or false; also it returns position of as a ref parameter.

  1. #include <string.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. bool FindS (string strMain, string strWord, int &pos){
  6.  
  7. if (strMain.find(strWord) == 'npos'){ //not found
  8. return (false);
  9. }
  10. else{ //found
  11. pos = strMain.find(strWord); //position
  12. return (true);
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Search a string of ','

 
0
  #4
Mar 12th, 2007
Tell me how the function you just posted would help make your code any simpler.

Plus you need to change 'npos' to string::npos if you want to have any chance of making it work.
Last edited by John A; Mar 12th, 2007 at 11:12 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: Valadair is an unknown quantity at this point 
Solved Threads: 0
Valadair Valadair is offline Offline
Newbie Poster

Re: Search a string of ','

 
0
  #5
Mar 13th, 2007
Originally Posted by Narue View Post
>I figured psuedocode would accomplish what I'm seeking.
You didn't specify if this is to be in C or C++. In C, look for the strchr function. In C++, look for the find_first_of member function of the string type.
I was looking for C++, sorry for not specifying. I did get it to work, and this is what I ended up using:

  1. if (dateStr.find(',') != string::npos)

Of course, there is more to the whole idea than that, but that's what I used in the control statement.

Thanks for pointing me in the right direction.

Thanks,
Val
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC