c++ strcmp a string array, how?

July 17, 2010 at 17:48:08
Specs: Windows XP Pro
not sure how to go about this, but that's why i'm asking.

not sure how to go about comparing and returning entries found in an array of char strings. Bascially i need to know the syntax of the areas <with annotations like this> in order to complete the following:

[code]
string appname;

char* cmdarray[5] = { "open", "bring up", "give me", "gimme", "launch" };
char& appnaamearray[2] = { "calc, notepad,"};

if (strcmp(vocal_input, <any match found in cmdarray>)==0 && strcmp(vocal_input, <any match found in appnamearray>)==0){
appname = <value of the match found in appnamearray>
open_app(appname);
}else if (<a match is found in cmdarray> && <no match is found in appnamearray>){
vocal_reply("Sorry, I don't recognize the name of the program that you want me to open.");
}
[/code]


See More: c++ strcmp a string array, how?

Report •

#1
July 18, 2010 at 04:14:21
You'll probably want to look at strstr(). But if you're really using C++, you should probably be using std::string instead of cstrings. In which case, you'd use std::string::find.

Report •

#2
July 18, 2010 at 04:41:57
currently using:

void ident_app_to_open(std::string user_input){
size_t found;
string appname;

//see if we want to open notepad
found=user_input.find("notepad");
if (found!=string::npos){
appname = "notepad";
open_app(appname);
}

found=user_input.find("note pad");
if (found!=string::npos){
appname = "notepad";
open_app(appname);
}


//see if we want to open the calculator
found=user_input.find("calculator");
if (found!=string::npos){
appname = "calc";
open_app(appname);
}

}

which works just fine. I was looking for a way to search for matches in an array such as:

const char* appnamearray[3] = " calc, notepad, note pad;"; instead of having to do a number of different statements like the the afore-mentioned.

As far as using std::string compared to string, to my knowledge (almost) all c commands work just fine in c++ and the compiler doesn't seem to throw any issues nor does it cause problems with application functionality.

I've seen people complain about the presense of c commands in a c++ app but i have yet to see anyone explain why it would potentially be an issue.

std::string
is pretty much the same as
string. std::string is just five more characters that seem to needlessly obfuscate matters.

I certianly wouldn't mind being corrected on the matter either. Though, i'm more importantly still looking for a way to use an array to list the commands and return matches to those commands located in another string to a variable.
it doesn't look like strstr does that. o.o;


Report •

#3
July 19, 2010 at 12:40:15
Though, i'm more importantly still looking for a way to use an array to list the commands and return matches to those commands located in another string to a variable.

In c? Bit rusty at c but this may help a bit:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  int i;
  const char *cmd = "open";
  char cmdname[16] = {0};
  const char *cmdarray[5] = {"open", "bring up", "give me", "gimme", "launch"};
  for(i=0; i<5; ++i) {
    if(strcmp(cmd, cmdarray[i]) == 0) strcpy(cmdname, cmdarray[i]);
  }
  if(*cmdname != '\0') {
    printf("%s\n", cmdname);
  } else {
    printf("No command\n");
  }
  system("PAUSE");	
  return 0;
}



Report •

Related Solutions

#4
July 22, 2010 at 12:55:21
i have yet to see anyone explain why it would potentially be an issue.
Mostly it's about bounds checking, overflows, type safety, and other memory management issues. That may or may not be an issue in your application. It's really dependent on how careful you are, and how complex the application is.

i'm more importantly still looking for a way to use an array to list the commands and return matches to those commands located in another string to a variable.
So you want to return a [cstring/string] if a match is found? You'll probably use [an array/a vector], and the for loop.

If you go the C array way, you'll need either a const number, or a #DEFINEd number. When you add/remove anything from the array, make sure to go back and modify your magic number appropriately.

If you use a vector (and its iterators), all you need to do is add the element to the vector:

#include <iostream>
#include <string>
#include <vector>
using std::string;

 //In: The string you're looking for
 //Returns: A string pointer to the match, or NULL if it's not found.
string const* findMatch(string const& s) {
   //Assumed: 's' has been processed: case changed to lower, 
   //non-alphanumeric removed, etc. If you plan on doing such work from this
   //function, remove the 'const&' from 's'.
  typedef std::vector<string> vecStr;
   //Vector is declared static for two reasons:
   //1: Subsequent runs will be faster; no need to build the vector
   //2: Our returned pointer will point to valid data.
   //A better way might be to make this vector global, or pass a vector around.
  static vecStr vs;
  if (vs.empty()) {
    vs.push_back("calc");
    vs.push_back("notepad");
    vs.push_back("paint");
    vs.push_back("wordpad");
  }
  
  for (vecStr::iterator it = vs.begin(); it != vs.end(); ++it)
    if (it->compare(s) == 0)
      return &(*it);
  
  return 0;
}

std::ostream& operator<< (std::ostream& o, string const* s) {
  using std::endl;
  if (s) o << *s << endl;
  else   o << "No match found" << endl;
  return o;
}
  
int main(int argc, char *argv[]) {
  using std::cout;
  cout << findMatch("notepad") << findMatch("paint") 
       << findMatch("BigBillsBassFishing") << findMatch("calc");
  system("pause");
}

Ah, almost 50 lines, and only 3 of them doing work (the for loop). I love C++.

Report •

#5
July 22, 2010 at 15:13:33
thanks for the explanations and assistance Razor! =)


Report •

Ask Question