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

Python019

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

}

空格不算是字符串,str=""这样的才算是空字符串,里面什么都没有,而str=" "是有内容了,这个str存储了一个字符(空格),如果你要把空格也算是空字符串,那么要稍作修改:

char str[] = " "

int len = strlen(str), i = 0

if (len >0)

{

while(i <len &&str[i++] == ' ')

if (i <len) // 字符串不空

else // 空字符串

}