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

  1. #1
    Registered User
    Join Date
    Jan 2004

    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
    strcmp, look it up.

    its in string.h

  3. #3
    Join Date
    Apr 2004
    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
    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


    Hello raiden,

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

    #include <string.h>
    #include <stdlib.h>
    int main() {
    	char *s1 = "Hello";
    	char *s2 = "Hello!";
    	char *s2 = "Hello";
    	if( !strcmp(s1, s2) )
    		printf("Strings did not match");
    	if( strcmp(s1, s3) == 0 ) // used ! last time to indicate 0
    		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, 10:13 AM
  2. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08: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, 09:17 PM