数组不打印其中的第一个字母

时间:2019-11-13 20:53:36

标签: c

我有一个由 stdin 导入的文本文件组成的数组。

文本文件如下所示:

"Name"
"Number"
"Name"
"Number"
...

整个代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>



int main(int argc, char** argv)
{
    //number of arguments

    if (argc > 2)
    {
        fprintf(stderr, "Too many arguments\n");
        return 1;
    }

    //check argument 1

    {
        if (argc == 2)
        {
            unsigned i = 0;

            while (i < strlen(argv[1]))
            {
                if ((isdigit(argv[1][i])) == 0)
                {
                    fprintf(stderr, "Enter a number\n");
                    return 1;
                }
                i++;
            }
        }
        else
        {
            fprintf(stderr, "argument\n");
            return -1;
        }
    }


    //find \n and separate

    int g = 0;
    int c = 0;
    char buffer[102];
    char people[42][102];
    char numbers[42][102];

    while (fgets(buffer, sizeof buffer, stdin) != NULL)
    {
        if (g % 2 == 0)
        {
            strcpy(people[c], buffer);
            //printf("%s", people[c]);
        }

        if (g % 2 == 1)
        {
            strcpy(numbers[c], buffer);
            c++;
        }
        g++;

    }

    //convert and remove \n

    char conv_people[42][102];
    for (int i = 0; i < c; i++)
    {
        for (unsigned j = 0; j < strlen(people[i]); j++)
        {
            if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
            {
                if (people[i][j] == '\n')
                {
                    conv_people[i][j] = '\0';
                }
                people[i][j] = conv_people[i][j] + 32;
            }

        }
    }

    //covert to numbers

    char conv[42][102];
    for (int i = 0; i < c; i++)
    {
        for (unsigned j = 0; j < strlen(people[i]); j++)
        {
            if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
            {
                conv[i][j] = '0';
            }
            if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
            {
                conv[i][j] = '2';
            }
            if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
            {
                conv[i][j] = '3';
            }
            if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
            {
                conv[i][j] = '4';
            }
            if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
            {
                conv[i][j] = '5';
            }
            if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
            {
                conv[i][j] = '6';
            }
            if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
            {
                conv[i][j] = '7';
            }
            if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
            {
                conv[i][j] = '8';
            }
            if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
            {
                conv[i][j] = '9';
            }
        }
    }

    //compare

    int i = 0;
    while (i < c)
    {
        if (strstr(conv[i], argv[1]) != NULL)
            printf("%s, %s", people[i], numbers[i]);
        if (strstr(numbers[i], argv[1]) != NULL)
            printf("%s, %s", people[i], numbers[i]);
        i++;
    }
    return 0;
}

该程序获取人员及其电话列表,并使用argv [1]进行搜索 输出总是省略每个单词中的第一个大写字母 因此,如果文件包含如下名称:Barrack Obama 程序返回arrack bama 数字和转换后的名称运行正常

我不想发布整个内容,因为它非常丑陋。

1 个答案:

答案 0 :(得分:1)

我已经运行了代码,并且John输出为Éohn。可能来自

people[i][j] = conv_people[i][j] + 32;

因为除了终结符外,您从未在conv_people[i]中设置任何值。

如果我在循环中添加第一行

strcpy(conv_people[i], people[i]);

然后是输出

john

带有小写首字母。

此外:使用起来更安全方便

people[i][j] = tolower(conv_people[i][j]);

甚至不需要测试是否通过了大写字母。