2017-06-15 43 views
-4

我写正确的代码为4的测试案例,但奋力写2测试用例出其中一个是一个隐藏的测试用例(字符串空间)检查字符串的特殊情况的第一个字母,并检查字符串与空间?

在礼品店,Tintu发现了很多花哨的文章刻有字母在里面。她决定为她的朋友选择礼物,这样他们的名字的第一个字母就会刻在礼物文章中。

礼品店有一个hifi扫描仪设备,可以照亮包含输入字母的文章。还有另一个接口设备可以选择用户输入的所有名字的第一个字母。突然,该设备开始出现故障,该店的店主Nisha非常紧张。 Tintu是Nisha的一位非常亲密的朋友,她提供帮助她解决这个问题。她开始重新编写程序以嵌入扫描仪之类的设备中。你能帮助她吗?

测试用例

输入1

MAHIRL

奇特拉

DEVI

Ç

输出1

输入2

MAHIRL

奇特拉

DEVI

输出2

没有

输入3

JON SNOW

ARYA STARK

HODOR

小号

输出3

没有

输入4

@ON SNOW

ARYA STARK

HODOR

@

输出4

没有

满足以下四个程序测试用例,但它在其中的两个失败(特殊字符和字符串空间)

#include<stdio.h> 
void main() 
{ 
    char a[256],b[256],c[256]; 
    char d[256]; 
    printf("Enter the first name"); 
    scanf("%s",&a); 
    printf("Enter the Second name"); 
    scanf("%s",&b); 
    printf("Enter the third name"); 
    scanf("%s",&c); 
    printf("Enter the first Letter"); 
    scanf("%s",&d); 
    if(a[0] == "[email protected]#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || b[0] == "[email protected]#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || c[0] == "[email protected]#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || d[0] == "[email protected]#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(") 
    printf("\nNo"); 
    if(d[0] == a[0] || d[0] == b[0] || d[0] == c[0]) 
    printf("\nYes"); 
    else 
    printf("\nNo"); 
} 
+2

编译该程序会引发疯狂的警告,也许你应该照顾这些。 –

+0

不确定第一个'if'语句试图做什么,但它不是有效的C,并且永远不会打印“否” –

+0

@ChrisTurner我试过这个语句来检查特殊情况。它没有工作。但它不会影响其他测试用例的输出。他们工作正常。 –

回答

0
#include<stdio.h> 
    #include<stdlib.h> 
    int main() 
    { 
     char a[256],b[256],c[256],d[256]; 

     fgets(a,256,stdin); 
     fgets(b,256,stdin); 
     fgets(c,256,stdin); 
     fgets(d,256,stdin); 
     if(d[0]==a[0]||d[0]==b[0]||d[0]==c[0]) 
     printf("\nyes"); 
     else 
     printf("\nno"); 
    } 

this will help you pass the string with space test case. 
-1
#include<stdio.h> 
#include<string.h> 
int main() 
{ 
char a[100],b[100],c[100],d[100]; 
gets(a); 
gets(b); 
gets(c); 
gets(d); 
if(d[0]>=0 && d[0]<=64) 
printf("no"); 
else if(a[0]==d[0] || b[0]==d[0] || c[0]==d[0]) 
printf("yes"); 
else 
printf("no"); 
} 
+1

欢迎来到Stack Overflow !谢谢你的代码片段,它可能会提供一些即时的帮助。通过显示*为什么*这是一个很好的解决方案,一个正确的解释[将大大提高](// meta.stackexchange.com/q/114762)其教育价值这个问题将会使未来的读者有更多的用处,但是这些问题会有相似但并非完全相同的问题。请编辑你的答案以增加解释,并指出适用的限制和假设。 –

0

我只是改变@prudhviraj的代码,我得到了解决,我需要。

谢谢@prudhviraj!

void main() 
{ 
    char a[256], b[256], c[256], d[256]; 

    fgets(a,256,stdin); 
    fgets(b,256,stdin); 
    fgets(c,256,stdin); 
    fgets(d,256,stdin); 

    if((a[0] < 'A' || a[0] > 'Z') || (b[0] < 'A' || b[0] > 'Z') || (c[0] < 'A' || c[0] > 'Z') || (d[0] < 'A' || d[0] > 'Z')) 
     printf("\nno"); 
    else if(d[0]==a[0] || d[0]==b[0] || d[0]==c[0]) 
     printf("\nyes"); 
    else 
     printf("\nno"); 
} 
+0

a [0]小于char A和[0]> char z对于所有字符串都是相同的 – HariSantosh

0
#include<stdio.h> 
#include<stdlib.h> 
int main() 
{ 
    char a[50],b[50],c[50],ch[50]; 
    fgets(a,50,stdin); 
    fgets(b,50,stdin); 
    fgets(c,50,stdin); 
    fgets(ch,50,stdin); 
    if((a[0]>='A' && a[0]<='Z') && (b[0]>='A' && b[0]<='Z') && (c[0]>='A' && c[0]<='Z') && (ch[0]>='A' && ch[0]<='Z')) 
    { 
     if(a[0]==ch[0] || b[0]==ch[0] ||c[0]==ch[0]) 
     { 
      printf("yes"); 
     } 
     else 
     { 
      printf("no"); 
     } 
    } 
    else 
    { 
     printf("no"); 
    } 
} 

此代码评估所有6例,其中特殊字符大小写和空格情况。

相关问题