Thread: comparing 2 strings with pointer

  1. #1
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22

    comparing 2 strings with pointer

    Hello all,

    I am trying to make a program that compares two strings, but I got stuck here
    I am a newbie in using pointer, can anyone help?

    here is the source code :

    Code:
    #include <stdio.h>
    
    int str_equal(const char *s1, const char *s2){
    	while(*s1 != '\0' && *s2 != '\0'){
    		if(*s1 != *s2){return 0;}
    		else{*s1++;*s2++;}
    	}
    	return 1;
    }
    
    
    int main(void){
    	char p, q;
    	
    	printf("Input a string : ");
    	scanf("%s",&p);
    	printf("Input another string : ");
    	scanf("%s",&q);
    	
    	if(str_equal(&p, &q)){
    		printf("The strings are the same.\n");
    	}else{
    		printf("The strings are not the same.\n");
    	}
    
    	return 0;
    }
    thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So exactly what is NOT working in your code?

    I suspect it may be that char p and q are only able to hold a single character. That can ONLY work if you have an empty string... For all other strings, you need a char array.

    When you pass an array, it turns into a pointer, so you don't need th &.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    Hi,
    Do you want to compare Char (s) or Strings ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char p, q;
    	
    printf("Input a string : ");
    scanf("%s",&p);
    printf("Input another string : ");
    scanf("%s",&q);
    Specifically, that. Those cannot be strings, unless you want your "string" to only be the null character. That's the only way you can consider a single character a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2
    If you want to compare strings ,in your main function you first need to be sure that those strings are well taken from user.

    i recommend you to define a maximum size for your strings, and use fgets to read string from standard input. you can better control what user gives.

    using 2 pointers on those string you can use you function to compare them.

  6. #6
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22
    actually it always returns 0 when I input more than one character..

    I tried to do this

    Code:
    char p[10], q[10];
    to determine the length of the array, but it got stuck in compiling process. It says "[Warning] passing arg 1 of `str_equal' from incompatible pointer type " and also in the second arg.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, remove the & on the call to str_equal [and scanf].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by meriororen View Post
    Hello all,

    I am trying to make a program that compares two strings, but I got stuck here
    I am a newbie in using pointer, can anyone help?

    here is the source code :

    Code:
    #include <stdio.h>
    
    int str_equal(const char *s1, const char *s2){
    	while(*s1 != '\0' && *s2 != '\0'){
    		if(*s1 != *s2){return 0;}
    		else{*s1++;*s2++;}
    	}
    	return 1;
    }
    
    
    int main(void){
    	char p[10], q[10];
    	
    	printf("Input a string : ");
    	scanf("%s",p);
    	printf("Input another string : ");
    	scanf("%s",q);
    	
    	if(str_equal(&p, &q)){
    		printf("The strings are the same.\n");
    	}else{
    		printf("The strings are not the same.\n");
    	}
    
    	return 0;
    }
    thanks in advance.
    You should do this.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your string function is actually a sort of strstr, not a strcmp function. Except for what it returns. But it functions more like strstr. At the end, if one string has reached the end, but not the other, it's still going to return 1.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22
    woops, I did it. I tried removing & and having a little change on the function.

    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. comparing strings
    By GanglyLamb in forum C Programming
    Replies: 5
    Last Post: 11-03-2002, 08:01 AM
  3. Comparing Strings
    By zdude in forum C Programming
    Replies: 1
    Last Post: 10-04-2002, 07:47 PM
  4. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 10:18 AM
  5. pointer and strings
    By lostpoet in forum C Programming
    Replies: 8
    Last Post: 03-08-2002, 11:51 AM