检查字符串的特殊情况的第一个字母并检查带空格的字符串?

时间:2017-06-15 14:32:10

标签: c

我为4个测试用例写了正确的代码但是很难写出2个测试用例,其中一个是隐藏的测试用例(带空格的字符串)

在礼品店里,Tintu发现了许多带有铭文的花哨文章。她决定为她的朋友选择礼物,以便他们的名字的第一个字母刻在礼物文章中。

礼品店里有一个高保真扫描仪设备可以照亮包含输入信件的文章。还有另一个接口设备可以选择用户输入的所有名称的第一个字母。突然,设备开始出现故障,商店老板Nisha非常紧张。 Tintu是Nisha非常亲密的朋友,她提出帮助她解决这个问题。她开始重写程序,将其嵌入扫描仪设备中。你能帮帮她吗?

  

测试用例

     

输入1

     

MAHIRL

     

奇特拉

     

DEVI

     

C

     

输出1

     

     

输入2

     

MAHIRL

     

奇特拉

     

DEVI

     

A

     

输出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] == "!@#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || b[0] == "!@#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || c[0] == "!@#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(" || d[0] == "!@#$%^&*+<>?|.,/[{]}\`~*:;'-_=)(")
    printf("\nNo");
    if(d[0] == a[0] || d[0] == b[0] || d[0] == c[0])
    printf("\nYes");
    else
    printf("\nNo");
}

4 个答案:

答案 0 :(得分: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 :(得分: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");
}

答案 2 :(得分: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个案例,包括特殊字符大小写和空格大小写。

答案 3 :(得分:-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");
}