comparing chars

This is a discussion on comparing chars within the C Programming forums, part of the General Programming Boards category; hii i want to compare 2 words... compare each letter i don't want to use strcmp ..this is the function ...

  1. #1
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    Thumbs down comparing chars

    hii
    i want to compare 2 words... compare each letter
    i don't want to use strcmp ..this is the function
    Code:
    int WordCompare(char *pFirst,char *pSecond){
       char *p1=pFirst,*p2=pSecond;int sum=0;
       if(WordLength(p1)!=WordLength(p2))
    	   return 0;
       else
          while(isalpha(*p1)){
    	      if(*p1==*p2)       // <== here is my problem..
    		      sum=sum+1;
    	      p1++; p2++;
    	      }
       if(sum==WordLength(p1))
    		   return 1;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if(*p1==*p2)       // <== here is my problem..
    What kind of a problem ?
    Kurt

  3. #3
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    jaxlle

    agin... i want to know how we compare 2 chars??
    "jaxlle"

    "jaxlle"

    j=j
    a=a
    ..............

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    size_t size1 = strlen(str1);
    size_t size2 = strlen(str2);
    int i,j;
    
    for(i=0;i < size1;i++)
        for(j=0;j < size2; j++)
            if(str1[i] == str2[j])
    there are lot of post on this topic on the board. U could just search the board for the solution. But here u go to start you.

    ssharish2005

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > for(i=0;i < size1;i++)
    > for(j=0;j < size2; j++)
    That's not it at all - that's more like strstr, not strcmp

    You want to advance a subscript at the same rate along both strings.
    say
    for ( i = 0 ; i < size1 && i < size2 ; i++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Salem
    > for(i=0;i < size1;i++)
    > for(j=0;j < size2; j++)
    That's not it at all - that's more like strstr, not strcmp

    You want to advance a subscript at the same rate along both strings.
    say
    for ( i = 0 ; i < size1 && i < size2 ; i++ )
    Salem i throught the same way before this code which i posted. This was my first code which i was suppose to post.

    Code:
    for(i=0; str1[i] != '\0' && str2[i] != '\0;i++)
    And i throught what if one string is less than the other and the compare stops inbetween. So i replaced it with the other which i posted. But u are right it much like strstr rather than to be strcmp

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Comparing Char's
    By MrKnights in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2007, 01:49 PM
  2. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  3. Comparing Chars
    By Jperensky in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 11:29 AM
  4. Comparing Chars
    By drdroid in forum C++ Programming
    Replies: 12
    Last Post: 02-26-2002, 01:06 PM
  5. comparing chars with pointer to char
    By papous in forum C Programming
    Replies: 5
    Last Post: 11-24-2001, 12:23 AM

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