strcmp()函数

strcmp()函数的简单介绍

一、strcmp()函数的简单介绍

strcmp是C/C++函数,用于比较两个字符串。
函数原型: int strcmp(const char *s1,const char *s2);
设这两个字符串为s1,s2,
规则
当s1<s2时,返回为负数
当s1=s2时,返回值= 0
当s1>s2时,返回正数
即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。如:
"A"<"B" "a">"A" "computer">"compare"

实现原理如下:首先比较两个串的第一个字符,若不相等,则停止比较并得出两个ASCII码大小比较的结果;如果相等就接着比较第二个字符然后第三个字符等等。无论两个字符串是什么样,strcmp函数最多比较到其中一个字符串遇到结束符'/0'为止,就能得出结果。
特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,即可用于比较两个字符串常量,或比较数组和字符串常量,不能比较数字等其他形式的参数。

二、代码举例

#include <syslib.h>
#include <string.h>
intmain()
{
    char *s1="Hello,Programmers!";
    char *s2="Hello,programmers!";
    int r;
    clrscr();
    r = strcmp(s1,s2);
    if(!r)
        printf("s1 and s2 are identical");
    else if(r<0)
        printf("s1 less than s2");
    else
        printf("s1 greater than s2");
    getchar();
    return0;
}

例题:NYOJ 题目25

A Famous Music Composer

时间限制:1000 ms  |  内存限制:65535 KB

难度:1

描述

Mr. B is a famous music composer. One of his most famous work was his set of preludes. These 24 pieces span the 24 musical keys (there are musically distinct 12 scale notes, and each may use major or minor tonality). The 12 distinct scale notes are: 

 A     A#=Bb  B        C       C#=Db D       D#=Eb  E       F        F#=Gb  G       G#=Ab

Five of the notes have two alternate names, as is indicated above with equals sign. Thus, there are 17 possible names of scale notes, but only 12 musically distinct notes. When using one of these as the keynote for a musical key, we can further distinguish between major and minor tonalities. This gives 34 possible keys, of which 24 are musically distinct. 

In naming his preludes, Mr. B used all the keys except the following 10, which were named instead by their alternate names: 

 Ab minor  A# major A# minor  C# major  Db minor
 D# major  D# minor Gb major  Gb minor  G# major 

Write a program that, given the name of a key, give an alternate name if it has one, or report the key name is unique. 

输入

Each test case is described by one line having the format "note tonality", where "note" is one of the 17 names for the scale notes given above, and "tonality" is either "major" or "minor" (quotes for clarify).

输出

For each case output the required answer, following the format of the sample.

样例输入

Ab minor
D# major
G minor

样例输出

Case 1: G# minor
Case 2: Eb major
Case 3: UNIQUE

解题代码:

#include <stdio.h>
#include <string.h>
char s1[10],s2[10];
int main (void)
{
   // freopen("题目25.txt","r",stdin);
    int n,i=1;
    while(scanf("%s %s",s1,s2)!=EOF)
    {
        getchar();
        printf("Case %d: ",i++);
        if(strcmp(s1,"A#")==0||strcmp(s1,"Bb")==0)
            printf("%s %s\n",(strcmp(s1,"A#")==0)?"Bb":"A#",s2);
        else if(strcmp(s1,"C#")==0||strcmp(s1,"Db")==0)
             printf("%s %s\n",(strcmp(s1,"C#")==0)?"Db":"C#",s2);
         else if(strcmp(s1,"D#")==0||strcmp(s1,"Eb")==0)
             printf("%s %s\n",(strcmp(s1,"D#")==0)?"Eb":"D#",s2);
             else if(strcmp(s1,"F#")==0||strcmp(s1,"Gb")==0)
             printf("%s %s\n",(strcmp(s1,"F#")==0)?"Gb":"F#",s2);
             else if(strcmp(s1,"G#")==0||strcmp(s1,"Ab")==0)
             printf("%s %s\n",(strcmp(s1,"G#")==0)?"Ab":"G#",s2);
             else printf("UNIQUE\n");
    }
 
    return 0;
}

注意输出    case(空格)i:(空格)%s(空格)%s

猜你喜欢

转载自blog.csdn.net/WWJ970529/article/details/81778909