【发布时间】:2015-02-04 17:34:56
【问题描述】:
你好,我是编程新手,想向你学习一些:) 我正在.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;
}
我希望你能告诉我我可以通过哪种方式做到这一点。 (我也是这个网站的新手。我搜索了这个但我找不到。对不起,如果它存在。)
【问题讨论】:
-
您希望结果字符串中的字母按任何特定顺序排列吗?
-
@5gon12eder -- 不,我只想将这些字符串组合成一个字符串,以消除多余的字母。
-
我建议创建堆栈数组 [21] 以考虑所有唯一字母(和空值)的可能性。
-
感谢@doppelheathen,已编辑
-
次要:
" %5s"中不需要该空间 - 你没有什么害处。许多(不是全部)scanf()格式说明符(例如"%s"、"%d"、"%f")都已准备好使用前导空白。
标签: c string letters combinators