LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2007, 10:51 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
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
 
Old 02-25-2007, 11:19 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 */
    }
  }
}
 
Old 02-25-2007, 11:23 AM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks for the quick response Dark_Helmet. I am going to try this out.

Regards,

Ben
 
Old 02-25-2007, 11:42 AM   #4
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
I have got it working. Thanks for the help Dark_Helmet.

Ben
 
Old 02-26-2007, 11:37 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
feel free to browse the source of this:

http://sourceforge.net/projects/shh-str-search/
 
Old 02-27-2007, 03:10 AM   #6
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
 
Old 02-27-2007, 03:50 AM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
 
Old 02-27-2007, 05:11 AM   #8
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
use strcpy, as argv[1] is a string
Code:
char var[10];
strcpy(var,(argv+1));
 
Old 02-27-2007, 05:18 AM   #9
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
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.
 
Old 02-27-2007, 06:06 AM   #10
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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

Last edited by daYz; 02-27-2007 at 06:08 AM.
 
Old 02-27-2007, 03:58 PM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.

Last edited by Dark_Helmet; 02-27-2007 at 04:07 PM.
 
Old 02-28-2007, 01:07 AM   #12
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
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))
 
Old 02-28-2007, 01:48 AM   #13
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
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.
 
Old 02-28-2007, 02:32 PM   #14
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
 
Old 03-01-2007, 01:19 PM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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
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,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
howto allow strings in header (.h) files? RHLinuxGUY Programming 1 05-20-2006 09:07 PM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 08:29 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
How to compare these two strings in one line code? powerplane Programming 4 07-10-2003 12:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration