两个字符串合并为一个字符串,它消除了在C相同的字母 [英] Combining two strings into one string which eliminates the same letters in C

查看:190
本文介绍了两个字符串合并为一个字符串,它消除了在C相同的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是在编程很新,想向你学习一些:)
我在做以.c程序,我在一个部分被卡住。
我想获得3个或更多的输入5个字符最大尺寸。 (例如:你好,HI,好,BYE)
我想他们堆放在那些4串持有相同的字母只有一次一个新的字符串
(例如:H,E,L,L,O,I,G,D,B,Y)

Hello i'm so new in the programming and want to learn some from you :) I'm doing a program in .c and i am stucked in a part. I want to get 3 or more inputs max size of 5 characters. (For example: HELLO, GOOD, BYE) And i want to stack them in a new string which holds same letters only once from those 4 strings (Example: 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;
}

我希望你能告诉我有哪些办法可以做到这一点。
(我也是在新的网站。我搜索了这一点,但我找不到。很抱歉,如果它的存在。)

I hope you can let me know with which way i can do it. (I'm also new in the site. I searched for this but i couldn't find. Sorry if it exists.)

推荐答案

首先在你的code一些意见:


  • 当别人在评论中已经已经提到的,你需要大小的缓冲区的 N 的+ 1举办的 N 的字符串。这是因为在C,一个字符串的长度不存储。相反,一个特殊的NULL字节追加到迎来了结束的字符串。因此,你的第一 ... 第四数组应该是长度至少为6。

  • 如果最坏的情况发生,用户输入每4个价值500字符不相交的话吗?然后合并后的字符串将计20个字符。所以,你的阵列应该能够容纳21个字符(再次终止NUL字节1)。 (也由<一提到href=\"https://stackoverflow.com/questions/27334684/combining-two-strings-into-one-string-which-eliminates-the-same-letters-in-c#comment-43126742\">user3121023's注释。)

  • 要使用 scanf函数,通过键入的char * 的参数读取字符串,而不是字符(*)[6] 第一已衰减到的char * 所以不要额外采取它的地址(如&放;第一个)。打开你的编译器警告(至少使用 -Wall )有关此类错误的通知。 (也由 Dere0405 提及,而我打字这个答案。)

  • 您使用 scanf函数的是不安全的。如果用户输入的字符串超过5个字符长,你会读到超出你的数组的末尾。你可以修改格式说明读%5S 来告诉 scanf函数停止第5个字符后阅读。然而,这将在该行的末尾留下多余的字符。更好的选择是使用 与fgets 函数getline 阅读输入的一整行。另外,简单地传递字符串作为命令行参数(我的preferred溶液)。

  • As somebody else has already mentioned in a comment, you need a buffer of size n + 1 to hold an n character string. This is because in C, the length of a string is not stored anywhere. Instead, a special NUL byte is appended to the string that marks its end. Therefore, your first, …, fourth arrays should be of length at least 6.
  • What if the worst case happens and a user enters four disjoint words each worth 5 characters? Then your combined string will count 20 characters. So your stack array should be able to accommodate 21 characters (again 1 for the terminating NUL byte). (Also mentioned by user3121023's comment.)
  • To read a string using scanf, pass an argument of type char *, not char (*)[6]. first already decays to char * so don't additionally take its address (as in &first). Turn on your compiler's warnings (use at least -Wall) to be informed about such bugs. (Also mentioned by Dere0405 while I was typing this answer.)
  • Your use of scanf is insecure. If the user inputs a string longer than 5 characters, you'll read beyond the end of your array. You could modify the format specifier to read %5s to tell scanf to stop reading after the 5th character. However, this will leave excess characters at the end of the line. A better option would be to use fgets or getline to read an entire line of input. Alternatively, simply pass the strings as command line arguments (my preferred solution).

我们的实际问题:

我不会给你一个完整的解决方案,但只有一些暗示,因为这看起来非常像功课。 (遗憾的是,有人已经给你完整的code,所以你可能会忽略我的答案。)

I won't give you a complete solution but only some hints because this looks very much like homework. (Unfortunately, someone else has already given you the full code so you will likely ignore my answer.)

您将不得不遍历所有五个字符串和检查每个字符,如果它已经被添加到。如果是的话,请继续,否则,就追加到。遍历字符串,我们可以用下面的习惯。

You'll have to loop over all five strings and check for each character if it was already added to the stack. If so, continue, otherwise, append it to the stack. To loop over a string, we can use the following idiom.

int i;
for (i = 0; first[i]; ++i)
  printf("The character at position %d is '%c'\n", i, first[i]);

可替换地,如果我们不需要参考当前索引,以下成语更加紧凑。

Alternatively, if we don't need to refer to the current index, the following idiom is more compact.

char * pos;
for (pos = first; *pos; ++pos)
  printf("The current character is '%c'\n", *pos);

请注意我们是如何使用的事实,第一 - 作为一个C字符串 - 终止与计算结果为false一个NULL字节。否则,我们就不知道在哪里停止迭代。

Note how we use the fact that first – being a C string – is terminated with a NUL byte which evaluates to false. Otherwise, we would not know where to stop iterating.

现在我们知道了如何遍历字符串中的字符,我们怎么能检查是否已经添加了一个人物?两种解决方案进入脑海:

Now that we know how to loop over the characters of a string, how can we check whether a character was already added? Two solutions come into mind:


  1. 遍历和每个元素比较有问题的当前字符。虽然你的短字符串,这可能是首选方法,它会成长为效率低下更长的字符串。

  1. Loop over stack and compare each element to the current character in question. While for your short strings, this might be the method of choice, it will grow inefficient for longer strings.

创建的每个字符计数器,并增加它时加入。您可以使用一个事实,即字符是只是数字。所以,你可以创建一个有256个元素的数组(有256个不同的字符 S)的所有初始设置为0,然后增量添加当前字符的位置。例如:

Create a counter for each character and increment it whenever added to stack. You can use the fact that chars are just numbers. So you could create an array with 256 elements (there are 256 different chars) all initially set to 0 and then increment the position for the character currently added. For example:

int counters[256];
memset(counters, 0, sizeof(counters));  /* fill with 0s */

和后来在code:

if (counters[(unsigned char) (*pos)]++)
  {
    /* Character was already added.  Do nothing. */
  }
else
  {
    /* Character was not added yet.  Add it to stack. */
  }

如果(计数器[(无符号字符)(* POS)] ++)是有点棘手。首先, * POS dreferences指针 POS 来得到当前字符中,然后PTED作为一个跨$ P $ unsigned char型因为数组不能有消极的指数。这一立场然后在专柜数组,如果语句中的评估抬头。最后,数值增加(但只有在比较),通过递增运算符。

The if (counters[(unsigned char) (*pos)]++) is a little tricky. First, *pos dreferences the pointer pos to yield the current character which is then interpreted as an unsigned char because arrays cannot have negative indices. That position is then looked up in the counters array and evaluated in the if statement. Finally, the value is incremented (but only after comparing) via the post-increment operator.

不要忘了终止,并在最后一个NULL字节。

Don't forget to terminate stack with a NUL byte at the end.

这篇关于两个字符串合并为一个字符串,它消除了在C相同的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆