Thread: help with write your own strcmp function

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    help with write your own strcmp function

    hi there, i'm trying to write my own string compare function for preparation for exams and i need some advice. i don't understand how to call the string compare can you please help me out it would be very appreciated!

    This should be a program if s before t it should produce 1 , if is identical it should produce 0 and if t after s it should produce - 1 and i also wanted to know how to use char - char with ascii codes


    Code:
    int main () {
        char *s [80]= {"fish"};
        char *t [80] = {"flash"};
        if string_compare(*s < *t)
            printf("<0");
        if string_compare(*s > *t)
            printf(">0");        
        if string_compare(*s == *t)
            printf("0");
    }
    int string_compare(char *s, char *t){
        int i;
        for (i = 0; s[i] == t[i]; i++)
            if (s[i] == '\0')
                return 0;
            return s[i] - t[i];
    }
    Last edited by Iris Li; 05-03-2012 at 08:02 AM.

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by Iris Li View Post
    hi there, i'm trying to write my own string compare function for preparation for exams and i need some advice. i don't understand how to call the string compare can you please help me out it would be very appreciated!

    This should be a program if s before t it should produce 1 , if is identical it should produce 0 and if t after s it should produce - 1 and i also wanted to know how to use char - char with ascii codes


    Code:
    int main () {
        char *s [80]= {"fish"};
        char *t [80] = {"flash"};
        if string_compare(*s < *t)
            printf("<0");
        if string_compare(*s > *t)
            printf(">0");        
        if string_compare(*s == *t)
            printf("0");
    }
    int string_compare(char *s, char *t){
        int i;
        for (i = 0; s[i] == t[i]; i++)
            if (s[i] == '\0')
                return 0;
            return s[i] - t[i];
    }
    So let me get this straight. You wrote your very own implementation, but yet you don't know how to correctly declare a char array, how to call a function, or how to write code in main that makes any logical sense? More likely, you never actually studied the language you're working with, ripped the code off from some website, and now want us to come to your rescue so that you can make the grade. FAIL!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 01:25 AM
  2. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  3. question about function strcmp() in C
    By thungmail in forum C Programming
    Replies: 5
    Last Post: 03-13-2008, 05:46 AM
  4. write your own version of strcmp help..
    By webznz in forum C Programming
    Replies: 7
    Last Post: 10-28-2007, 02:06 PM
  5. help w/ write() function and open() function
    By Landroid in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 11:38 AM