c语言判断怎么判断字符串?

Python020

c语言判断怎么判断字符串?,第1张

参考代码如下:

#include<stdio.h>

int main()

{

char str[300]="2y1"

char c

int i=0,flag=0

c=str[0]

if(c=='Y'||c=='N'||c=='2'){

    //M1先说话

    flag=1

    for(){

    switch(c){

    case 'Y':

    case 'N':

    c=str[i+1] 

    if(c!='Y'&&c!='N'&&c!='2') 

    flag=0

    break

    case '2':

    c=str[i+1]

    if(c!='y'&&c!='n'&&c!='1'&&c!=0) 

    flag=0

    break

    case 'y':

    case 'n': 

    c=str[i+1] 

    if(c!='y'&&c!='n'&&c!='1') 

    flag=0

    break 

    case '1': 

    c=str[i+1] 

    if(c!='Y'&&c!='N'&&c!='2'&&c!=0) 

    flag=0

    break 

    case 0: break

    default: flag=0

    }

    if(flag==0||str[i]==0)

    break

    else{

    ++i

    c=str[i]

    }

    }

}

if(flag==1)

printf("%s是对话",str)

else

printf("%s不是对话",str) 

return 0

}

C语言提供了几个标准库函数,可以比较两个字符串是否相同。以下是用strcmp()函数比较字符串的一个例子:\x0d\x0a \x0d\x0a#include \x0d\x0a#include \x0d\x0avoid main (void)\x0d\x0avoid main(void)\x0d\x0a{\x0d\x0achar* str_1 = "abc" char * str_2 = "abc" char* str_3 = "ABC" \x0d\x0aif (strcmp(str_1, str_2) == 0)\x0d\x0aprintf("str_1 is equal to str_2. \n")\x0d\x0aelse\x0d\x0aprintf("str_1 is not equal to str_2. \n")\x0d\x0aif (strcmp(str_1, str_3) == 0)\x0d\x0a printf("str_1 is equal to str_3.\n")\x0d\x0aelse\x0d\x0aprintf("str_1 is not equalto str_3.\n");\x0d\x0a}\x0d\x0a\x0d\x0a上例的打印输出如下所示: \x0d\x0astr_1 is equal to str_2. \x0d\x0astr_1 is not equal to str_3.\x0d\x0a \x0d\x0astrcmp()函数有两个参数,即要比较的两个字符串。strcmp()函数对两个字符串进行大小写敏感的(case-sensitiVe)和字典式的(lexicographic)比较,并返回下列值之一:\x0d\x0a----------------------------------------------------\x0d\x0a返 回 值 意 义\x0d\x0a----------------------------------------------------\x0d\x0a0 第一个字符串大于第二个字符串\x0d\x0a----------------------------------------------------\x0d\x0a在上例中,当比较str_1(即“abc”)和str_2(即“abc”)时,strcmp()函数的返回值为0。然而,当比较str_1(即"abc")和str_3(即"ABC")时,strcmp()函数返回一个大于0的值,因为按ASCII顺序字符串“ABC”小于“abc”。\x0d\x0astrcmp()函数有许多变体,它们的基本功能是相同的,都是比较两个字符串,但其它地方稍有差别。下表列出了C语言提供的与strcmp()函数类似的一些函数: \x0d\x0a-----------------------------------------------------------------\x0d\x0a函 数 名 作 用\x0d\x0a-----------------------------------------------------------------\x0d\x0astrcmp() 对两个字符串进行大小写敏感的比较\x0d\x0astrcmpi()对两个字符串进行大小写不敏感的比较\x0d\x0astricmp()同strcmpi()\x0d\x0astrncmp()对两个字符串的一部分进行大小写敏感的比较\x0d\x0astrnicmp() 对两个字符串的一部分进行大小写不敏感的比较\x0d\x0a-----------------------------------------------------------------\x0d\x0a在前面的例子中,如果用strcmpi()函数代替strcmp()函数,则程序将认为字符串“ABC”等于“abc”。