gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:17:02 26 4
gpt4 key购买 nike

我有一个由 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该程序返回阿拉克巴马数字和转换后的名称工作正常

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

最佳答案

我已经运行了代码,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]);

甚至不需要测试来查看是否传递了大写字母。

关于c - 数组不打印其中的第一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58845444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com