Compare contents of 2 strings to see if they are the same?

This is a discussion on Compare contents of 2 strings to see if they are the same? within the C Programming forums, part of the General Programming Boards category; How do i do this? Im new to C and i need to do this on an assignment im working ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Compare contents of 2 strings to see if they are the same?

    How do i do this?

    Im new to C and i need to do this on an assignment im working on.

    Any help is much appreciated.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    strcmp, look it up.

    its in string.h

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    vous pouvez declarer une chaine de caractere dans un tableau de la facon suivante
    #define max 10000
    char t[max];
    puis lire de cette facon
    scanf("%c",&t[]);
    la comparaison sera faite dans une boucle repeter caractere par caractere
    je croit que ca c'est tre facile alors faite le vite

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hello raiden,

    I also recommend strcmp(), let me write an example:

    Code:
    #include <string.h>
    #include <stdlib.h>
    
    int main() {
    	char *s1 = "Hello";
    	char *s2 = "Hello!";
    	char *s2 = "Hello";
    
    	if( !strcmp(s1, s2) )
    		printf("Match!");
    	else
    		printf("Strings did not match");
    
    	if( strcmp(s1, s3) == 0 ) // used ! last time to indicate 0
    		printf("Match!");
    	else
    		printf("Strings did not match");
    
    	return 0;
    }
    Code 1.1: Example of using strcmp()

    What I did here was make three variables, *s1, *s2, and *s3. s1 and s3 are the same, while s2 varies from the group. strcmp returns 0 if the strings match, else > 1 depending on the difference of the two strings.

    It's quite a simple implementation, so either way you use it "== 0" or "!" (which indicates 0) will still produce the same result. I personally like the "!" myself, but every programmer has his/her ways of coding format and style.


    Hope this helped,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 11:13 AM
  2. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  3. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  4. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 10:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21