您好,我在编程方面很新,想要向您学习一些:) 我在.c做一个程序而且我被困在一个部分。 我希望获得3个或更多输入,最大大小为5个字符。 (例如:HELLO,HI,GOOD,BYE) 我想将它们堆叠在一个新的字符串中,该字符串只能从这4个字符串中保存一次相同的字母 (例如:H,E,L,L,O,I,G,D,B,Y)
#include <stdio.h>
#include <string.h>
int main(void) {
char first[5], second[5], third[5], fourth[5];
printf("Enter 1st word: \n"); scanf(" %5s", &first);
printf("Enter 2nd word: \n"); scanf(" %5s", &second);
printf("Enter 3rd word: \n"); scanf(" %5s", &third);
printf("Enter 4th word: \n"); scanf(" %5s", &fourth);
char stack[21]; // i want a new string like this and then combine first 4 strings
// in this string...
return 0;
}
我希望你能告诉我哪种方式可以做到。 (我也是网站上的新人。我搜索了这个,但我找不到。抱歉,如果它存在。)
答案 0 :(得分:1)
首先对您的代码发表一些评论:
first
,...,fourth
数组的长度至少应为6。stack
数组应该能够容纳21个字符(对于终止NUL字节,再次为1)。 (user3121023的评论也提到了。)scanf
读取字符串,请传递char *
类型的参数,而不是char (*)[6]
。 first
已经衰减到char *
,因此不会另外采用其地址(如&first
中所述)。打开编译器的警告(至少使用-Wall
)以了解此类错误。 (当我输入这个答案时,Dere0405也提到了。)scanf
的使用是不安全的。如果用户输入的字符串长度超过5个字符,您将读取超出数组末尾的字符串。您可以修改格式说明符以读取%5s
以告知scanf
在第5个字符后停止读取。但是,这会在行尾留下多余的字符。更好的选择是使用fgets
或getline
来读取整行输入。或者,只需将字符串作为命令行参数传递(我的首选解决方案)。现在解决实际问题:
我不会给你一个完整的解决方案但只提供一些提示,因为这看起来非常像家庭作业。 (不幸的是,其他人已经给你完整的代码,所以你可能会忽略我的答案。)
你必须循环遍历所有五个字符串并检查每个字符是否已添加到stack
。如果是,请继续,否则,将其附加到stack
。要循环一个字符串,我们可以使用以下习语。
int i;
for (i = 0; first[i]; ++i)
printf("The character at position %d is '%c'\n", i, first[i]);
或者,如果我们不需要引用当前索引,则以下习语更紧凑。
char * pos;
for (pos = first; *pos; ++pos)
printf("The current character is '%c'\n", *pos);
请注意我们如何使用first
- 作为C字符串 - 以NUL字节终止的事实,该字节的计算结果为false。否则,我们不知道在哪里停止迭代。
现在我们知道如何循环字符串的字符,我们如何检查字符是否已被添加?我想到了两个解决方案:
循环stack
并将每个元素与当前有问题的字符进行比较。虽然对于你的短字符串,这可能是选择的方法,但对于更长的字符串,它会变得效率低下。
为每个字符创建一个计数器,并在添加到stack
时递增。您可以使用char
s只是数字的事实。因此,您可以创建一个包含256个元素的数组(有256个不同的char
s),这些元素最初都设置为0,然后递增当前添加的字符的位置。例如:
int counters[256];
memset(counters, 0, sizeof(counters)); /* fill with 0s */
然后在你的代码中:
if (counters[(unsigned char) (*pos)]++)
{
/* Character was already added. Do nothing. */
}
else
{
/* Character was not added yet. Add it to stack. */
}
if (counters[(unsigned char) (*pos)]++)
有点棘手。首先,*pos
引用指针pos
以产生当前字符,然后将其解释为unsigned char
,因为数组不能具有负索引。然后在counters
数组中查找该位置,并在if
语句中进行评估。最后,通过后增量运算符增加值(但仅在比较之后)。
不要忘记在最后用NUL字节终止stack
。
答案 1 :(得分:0)
请按以下步骤更新您的代码:
printf("Enter 1st word: \n"); scanf(" %s", &first);
到
printf("Enter 1st word: \n"); scanf(" %s", first);
请更新到其他行。
答案 2 :(得分:0)
尝试:
#include <stdio.h>
#include <string.h>
int main(void) {
char strings[4][6];
printf("Enter 1st word: \n"); scanf(" %s", strings[0]);
printf("Enter 2nd word: \n"); scanf(" %s", strings[1]);
printf("Enter 3rd word: \n"); scanf(" %s", strings[2]);
printf("Enter 4th word: \n"); scanf(" %s", strings[3]);
char stack[21]; // This needs to be pretty much bigger than all combined just in case
// Initialize the stack string to 0 length
stack[0] = '\0'
// Move through each word
for(unsigned char word = 0; word < 4; word++){
// Move through each letter of each word
for(unsigned char letter = 0; letter < strlen(strings[word]); letter++){
// Test to see if the current letter is within the current stack string
unsigned char schar;
for(schar = 0; schar < strlen(stack); schar++){
if(stack[schar] == strings[word][letter]) break;
}
if(schar >= strlen(stack)){
unsigned char sstacklen = strlen(stack);
stack[sstacklen] = strings[word][letter];
stack[sstacklen+1] = '\0';
}
}
}
return 0;
}
这应该适用于你想要的东西,这是一个快速写入,所以可能会有小错误!