Thread: Quick Newbie String Help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Quick Newbie String Help

    I have an assignment in C where I need to create a function to compare 2 strings to see if they are equal without using any of the standard library functions for strings. I have the following so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int equals(char,char);
    
    int main(void){
    
    char s1[] = "PRINT";
    char s2[12];
    
    (void)scanf("%s",s2);
    
    if(equals(s1,s2) == 1)
    printf("s2 = PRINT");
    else
    printf("s2 != PRINT");
    
    }
    How would I create the equals function?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    By walking along the arrays simultaneously and comparing each char of s1 with each char of s2 in turn until end of string (NULL) is reached.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    However, for the equals function to work correctly, it probably should take a char [] or char * argument for the two parameters. As it is used right now, it shouldn't compile (or at least, it will not work right to compare the entire string, even if it compiles).

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Newbie: having a problem with my string function.
    By dsharp in forum C Programming
    Replies: 3
    Last Post: 10-20-2003, 10:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM