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:

if (string contains a ',')
   //do this

else //string contains no ','
  //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

>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.

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.

#include <string.h>
#include <iostream>
using namespace std;

bool FindS (string strMain, string strWord, int &pos){
     
      if (strMain.find(strWord) == 'npos'){  //not found
                           return (false);
                           }
      else{                                 //found
           pos = strMain.find(strWord);       //position
             return (true);                        
                         }        
}

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.

>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:

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

This article has been dead for over six months. Start a new discussion instead.