Thread: searching a string for mulitple characters

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    searching a string for mulitple characters

    hi,

    I am writting a program which has to search through strings. Up until now I have been using the code

    string.find_first_of("a")

    for example. The problem now is that I now need to search through for one of four characters + - / * and return the position of the first symbol it comes across or -1 if it can not find any.

    Can I do this using the find_first_of method or do I have to do it another way ??

    Thank you for your help

    Craig

  2. #2
    Alipha
    Guest
    (I'm not familiar with the find_first_of() method, but I assume it returns the character position in the string or -1 if not found)

    Well, you can find the first instance of +, -, *, and / then compare them to see which appeared first:

    plus = string.find_first_of("+");
    minus = string.find_first_of("-");
    multiply = string.find_first_of("*");
    divide = string.find_first_of("/");

    first = -1;

    if(plus != -1)
    if((first == -1) || (plus < first))
    first = plus;
    if(minus != -1)
    if((first == -1) || (minus < first))
    first = minus;
    if(multiply != -1)
    if((first == -1) || (multiply < first))
    first = multiply;
    if(divide != -1)
    if((first == -1) || (divide < first))
    first = divide;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM