C Language:: Simulation implements STRCMP function

Source: Internet
Author: User
Tags strcmp

Topic Requirements

Write a C-language program simulation to implement the STRCMP function.

(We still simulate the implementation of the STRCMP function, and then against the String.h library in the implementation of the STRCMP function, compared with the Master gap.)

Algorithm analysis

Through the previous article: C:: strcmp functions, prototypes, usages and examples we obtained the following information for the STRCMP function:

STRCMP prototype: int strcmp (const char *S1, const char *S2);

strcmp function: Compares two strings from left to right by character (according to the ASCII value) until a different character or ' s ' is encountered.

strcmp return value: S1>s2 return value >0, s1<s2 return value <0, s1=s2 return value =0

After we understand the general information of the strcmp function, it is not difficult to analyze the algorithm of simulating the implementation of strcmp function.

Algorithm Summary

Loops through S2, compared to S1. If equal, continue the comparison of the next character; otherwise, return the content that the S1 points to-s2

Simulation Implementation Core Code
The simulation implements the STRCMP function int my_strcmp (const char *S1, const char *S2) {//Assert assert (S1) for S1;//Assert assert (S2) for S2;// When the value that S1 points to is equal to the value that S2 points to//and the value that S2 points to is not \ 0 o'clock into the loop while (!) ( *S1-*S2) &&*s2) {s1++;s2++;} return *S1-*S2;}
String.h Library Core Code
String.h Library in strcmp function core code int __cdecl strcmp (const char *s1,const char *s2) {    int ret = 0;    while (!) ( RET = (* (unsigned char*) S1-* (unsigned char*) s2) && *s1) {        ++s1,++s2;    }    if (ret<0) {        ret =-1;    }    if (ret>0) {        ret = 1;    }    return (ret);}
Full Test code
/** This code by the high minor original, the copyright High Minor Blog All * Author: High Minor * Date: 2016-8-9* code function: Simulation Implementation STRCMP function * Integrated development environment: Microsoft Visual Studio */#include < stdio.h> #include <stdlib.h> #include <assert.h>//simulation implementation strcmp function int my_strcmp (const char *S1, const char * S2) {//Assert assert (S1) for S1,//Assert assert on S2 (S2),//when S1 points to a value equal to S2 point//and S2 points to a value other than \ 0 o'clock into the loop while (!) *S1-*S2) &&*s2) {s1++;s2++;} return *S1-*S2;}    String.h Library in strcmp function core code int __cdecl strcmp (const char *s1,const char *s2) {int ret = 0; while (!) (    RET = (* (unsigned char*) S1-* (unsigned char*) s2)) && *s1) {++s1,++s2;    } if (ret<0) {ret =-1;    } if (ret>0) {ret = 1; } return (ret);} int main () {//a day, Chinese, American, Indian, Japanese travel together//accidentally walk to a deserted place, met the cannibal chiefs/Chiefs said than the Chinese ...    The short man left as my supper!    Char chinese[]= "I 18cm";    Char american[]= "I 19cm";    Char indian[]= "I 18cm";    Char japanese[]= "I 15cm";    Who do you think it is?    printf ("Judgment sickle: The final output of less than 0 people will be eaten by cannibals!\n");    printf ("american:%d\n", my_strcmp (American,chinese)); printf ("indian:%d\n", my_strcmp (INdian,chinese));    printf ("japanese:%d\n", my_strcmp (Japanese,chinese));    System ("pause"); return 0;}
Output

      List of functions
    1. printf () function functions, prototypes, usages, and instances
    2. ASSERT () macro features, prototypes, usages, and instances
    3. STRCMP () functions, prototypes, usages and examples

I wish you all the best in a minor.

This article by high minor blog original!

Original address: http://gaoxiaodiao.com/p/23.html

If you want to reprint, please specify the source!

C Language:: Simulation implements STRCMP function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.