strcmp and/or strncmp. issues

Ok, I have this program pretty much done but I'm just trying to tweak it up a bit.

I'm writting a commmand line interpreter and trying to get it recognize as few characters as possible for the commands. For example I have:

1
2
3
4
5
6
7
8
9
10
11
	    if(strcmp(arguments[0],"assemble") == 0)
	    {
		if(argcount!=2)
		{
		    printf("Need exactly one argument! See help for more info.\n");
		}
		else
		{
		    printf("Assemble SIC program into file %s.\n",arguments[1]);
		}
	    }


I want it to recognize "a" or "assemble" or even "assem" if possible as well as know that "at" is not the assemble command. I tried using strncmp, but can't get it work how I want it to. I appreciate any help or suggestions. If you need to see more of what I have, just let me know.
argv[0] is the name of the binary.
argv[1] is the first parameter passed. And so on.
argc is never lower than 1.
Last edited on
You'll also need to be careful that each argument string is unique.

The function you want is strstr().
1
2
3
4
bool prefix_match( const char* arg, const char* pattern )
  {
  return strstr( pattern, arg ) == pattern;
  }

Now:
1
2
3
4
prefix_match( "foo", "fooey"  ) --> true
prefix_match( "ey",  "fooey"  ) --> false
prefix_match( "zaz", "fooey"  ) --> false
prefix_match( "foo", "foobar" ) --> also true

Hope this helps.
Thanks Duoas, I will give this a try!
Duoas,

I am still having problems with it working. Can I email you my full so you can take a look at it? I dont' want to post the whole thing on here.
Er, sure. No promises though. If it is huge just put a link to it instead of filling-up my inbox.
Topic archived. No new replies allowed.