1

I wanna create a list of 50 elements which consist of four chars each. Every four char string should go into a loop one by one and get checked for one of three letters (o, a, e) anywhere in the current string. dependent on whether or not these letters are identified different commands are executed

I tried all day im frustrated please help me....

3
  • 2
    Posting some code about what you tried will help... Commented Jul 26, 2009 at 19:51
  • 2
    You should be more specific on the requirements. Is the decision on only any of the characters being found or it also depends con which character it was? Can strings contain more than one of the characters and what should be done then (are there specific operations for combinations)? Commented Jul 26, 2009 at 21:54
  • THANK YOU ALL its been a great help! Yeah you are right i should be more precise with my questions sorry i was tired and desperate :D Commented Jul 29, 2009 at 14:12

6 Answers 6

3
typedef std::list<std::string> MyList;

MyList myList = getMyList();

MyList::const_iterator i = myList.begin(), iEnd = myList.end();

for (; i != iEnd; ++i) {
    const std::string& fourChars = *i;
    if (fourChars.length() == 4) {
        std::string::const_iterator j = fourChars.begin(), 
                                    jEnd = fourChars.end();
        for (; j != jEnd; ++j) {
            char c = *j;
            switch (c) {
                case 'o': case 'O': doO(); break;
                case 'a': case 'A': doA(); break;
                case 'e': case 'E': doE(); break;
                default: // not oae
            }
        }
    }
    else {
        // not 4 chars, what should we do?
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can go as:


#define NUM_ELEMENTS 50
#define WIDTH 4

// your function
char list[NUM_ELEMENTS][WIDTH];
//initialize list
for(i=0 ; i < NUM_ELEMENTS ; i++ )
 for(j=0 ; j < WIDTH ;j++)
  {
   switch(list[i][j])
    {
     case 'o': // execute command
             break;

     case 'a': // execute command
             break;

     case 'e': // execute command
             break;
    }
  }


1 Comment

const int NumberElements > #define NUM_ELEMENTS
1

You could also use some string STL function:

list<const string> theStringList; 
// fill list somehow
for(list<const string>::iterator it = theStringList.begin();
    it != theStringList.end();
    ++it) {
        const string& aString = *it;
        // assuming lower-case letters only
        if (aString.find_first_of("a",0) != string::npos) 
            doAStuff();
        else if (aString.find_first_of("e",0) != string::npos) 
            doEStuff();
        else if (aString.find_first_of("o",0) != string::npos) 
            doOStuff();
        else 
            ;// error handling or no-op
}

2 Comments

Nice to use the STL! You could even limit this to just 1 find_first_of("aeoAEO") call. Why would you prefer list over vector if insertion isn't mentioned in the requirement?
While the topic said "array", the question text said "list". Using the whole "aeoAEO" string would not allow you to provide different behaviour w.r.t. the character found, would it?
0
#include <list>
#include <string> 
#include <boost/foreach.hpp>

using std::list;
using std::string

...

list<string> strings;

// Populate list here

BOOST_FOREACH(string s, strings) {
    bool hasOAE = false;
    BOOST_FOREACH(char c, s) {
        if(c == 'o' || c == 'a' || c == 'e') {
            hasOAE = true;
        }
    }
    if(hasOAE) {
        doSomethingWith(s);
    } else {
        doSomethingElseWith(s);
    }
}

Comments

0

What you seem to need is

  • a structure representing a 4-char string (e.g. char[4] or std::string), e.g. called "Element".

  • an array of 50 elements of this type (e.g. std::vector or a play array), e.g. called "ElementContainer"

  • a loop iterating over each Element of the ElementContainer and feeding it into a processing function

  • a function accepting that Element, and finding either of the mentioned characters.

Which of these are you having trouble with?

Example:

 typedef char[4] Element1;
 typedef std::string Element2;

 struct Element3 { // slightly OO
     char[4] chars;
     bool has( char c ) const { 
       return std::find( chars, chars+4, c ) != chars+4; 
     }
 };


// a container
Element3 elements[] = {
"adda", "bebe", "xxpo", ...
};
Element3* afterlast = elements + sizeof(elements)/sizeof(elements[0]);


// a function:
void dispatch( Element3& element ) {
  if( element.has( 'a' ) ) return do_a();
  if( element.has( 'e' ) ) return do_e();
  if( element.has( 'o' ) ) return do_o();
}

//iteration
std::for_each( elements, afterlast, &dispatch );

Comments

0

I'm a huge fan of the Qt4 framework, specially for working on cross-platform solutions.

    QStringList list;
list << "abcd" << "zoeb" << "dbca" << "xedz" << "zbco" << "zzzz";
foreach (QString str, list) {
    if(str.indexOf("a", Qt::CaseInsensitive) != -1) {
        do_a(str);
    } else if(str.indexOf("e", Qt::CaseInsensitive) != -1) {
        do_e(str);
    } else if(str.indexOf("o", Qt::CaseInsensitive) != -1) {
        do_o(str);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.