LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Howto compare two strings in C (http://www.linuxquestions.org/questions/programming-9/howto-compare-two-strings-in-c-532258/)

daYz 02-25-2007 10:51 AM

Howto compare two strings in C
 
Hi,

I want to compare two strings in C, where one string is recieved from a program's argument, and the other comes from a variable. I want to execute a command when the strings are equal.

Can someone show me how that can be done please? I have tried several things but I can't get it to work.

Thanks a lot.

Ben

Dark_Helmet 02-25-2007 11:19 AM

man strcmp

example:
Code:

#include <string.h>

int main( int argc, char*argv[] )
{
  char *local_string;

  /* Do something to put a value into local_string */

  /* Check to make sure we actually got a commandline argument */
  if( argc > 1 )
  {
    /* Compare the contents of the two strings */
    if( strcmp( argv[1], local_string ) == 0 )
    {
      /* The two strings are identical here - Do your other stuff */
    }
    else
    {
      /* The two strings are different here */
    }
  }
}


daYz 02-25-2007 11:23 AM

Thanks for the quick response Dark_Helmet. I am going to try this out.

Regards,

Ben

daYz 02-25-2007 11:42 AM

I have got it working. Thanks for the help Dark_Helmet.

Ben

schneidz 02-26-2007 11:37 AM

feel free to browse the source of this:

http://sourceforge.net/projects/shh-str-search/

daYz 02-27-2007 03:10 AM

Quote:

Originally Posted by schneidz
feel free to browse the source of this:

http://sourceforge.net/projects/shh-str-search/

Hey that looks good schneidz. I am going to try that out.

Ben

daYz 02-27-2007 03:50 AM

I would like to assign the programs argument to a variable and then use that variable with the strcmp command, instead of using argv[1] directly, but I am unable to achieve that.

What is the proper way of doing this? I can't figure it out.

Thanks

Ben :jawa:

varun_shrivastava 02-27-2007 05:11 AM

use strcpy, as argv[1] is a string
Code:

char var[10];
strcpy(var,(argv+1));


slzckboy 02-27-2007 05:18 AM

should it not be ?

char var[10];
strcpy(var,*(argv+1));

by the way would would ideally want to make sure that argv[1] will fit inside var b4 copying as strcpy will not check to see if there is space.

Try it and see.

:0)

make your program arguement longer than 10 characters and the program will more than likely seg fault.

daYz 02-27-2007 06:06 AM

Thanks a lot for the help guys. I will try this out this afternoon.

Ben http://www.artis.nl/paginas/t/dieren...img/keizer.jpg

Dark_Helmet 02-27-2007 03:58 PM

Space does need to be allocated for the local variable receiving the copy. This is code I typically use:
Code:

arg_length = strlen( argv[i] );
arg_copy = malloc( ( arg_length + 1 ) * sizeof( char ) );
memset( arg_copy, 0, (arg_length + 1) * sizeof( char ) );
strncpy( arg_copy, argv[i], arg_length + 1 );

/* Do other stuff... */

free( arg_copy );
arg_length = 0;

EDIT:
Of course, the above code assumes a number of things:
1. arg_copy is defined as a char *
2. arg_length is some sort of integer values (I always use "unsigned" )
3. the variable 'i' has been properly declared and initialized
4. there is a command-line argument corresponding to i's value
5. the malloc() call succeeds
6. the memset() call succeeds
7. the strncpy() call executes without error (i.e. all chars copied)

Each step can be verified, but I'm too lazy to add it in and it would obfuscate the whole point of the code anyway.

varun_shrivastava 02-28-2007 01:07 AM

strcpy will take 2nd argument as *(argv+1) as strcpy takes a pointer as 2nd arg.

for variable var the size should be =
to strlen(*(argv+1))

slzckboy 02-28-2007 01:48 AM

Quote:

Originally Posted by varun_shrivastava
strcpy will take 2nd argument as *(argv+1) as strcpy takes a pointer as 2nd arg.

for variable var the size should be =
to strlen(*(argv+1))

I don't mean to keep butting in but the size of the destination buffer needs to be at least strlen(*(argv+1)) + 1 because strlen does not count the null terminator at the end of the string which you will need in your destination buffer,i.e var.

Code:

STRLEN(3)          Linux Programmer's Manual          STRLEN(3)

NAME
      strlen - calculate the length of a string

SYNOPSIS
      #include <string.h>

      size_t strlen(const char *s);

DESCRIPTION
      The strlen() function calculates the length of the string s, not including
      the terminating `\0' character.


daYz 02-28-2007 02:32 PM

I did not have the oppurtunity to try out your suggestions, but you have been really helpfull.

I'll try to post about the result tommorow.

Thanks again,

Ben

schneidz 03-01-2007 01:19 PM

Quote:

Originally Posted by daYz
I would like to assign the programs argument to a variable and then use that variable with the strcmp command, instead of using argv[1] directly, but I am unable to achieve that.

What is the proper way of doing this? I can't figure it out.

Thanks

Ben :jawa:

this is pretty much what strcpy does:
Code:

char out[256];
 for(int i = 0; i < strlen(argv[1]); i++)
  out[i] = argv[1][i];

hope this helps,


All times are GMT -5. The time now is 11:05 AM.