[英]Returns a new string with characters from two other strings
Write a function common_char that takes two strings as arguments and returns a new string that contains a single copy of all characters that appear in either of the two strings.编写一个函数 common_char,它接受两个字符串作为参数,并返回一个新字符串,该字符串包含两个字符串之一中出现的所有字符的单个副本。
For example, string1: hello ;例如,字符串 1:你好; string2: world ;字符串2:世界; the new string is : hellowrd ( o and l were already in array from hello).新字符串是: hellowrd ( o和l已经在 hello 的数组中)。
May use string function here.In other words, all characters in string1 are copied into the new string, but characters in string 2 are copied only characters that are not in string1.这里可以使用string函数。也就是说,string1中的所有字符都被复制到新的string中,但是string 2中的字符只复制string1中没有的字符。 That is past exam question and the university did not provide answer.那是过去的考试问题,大学没有提供答案。 Here is my code.这是我的代码。
#include <stdio.h>
#include <string.h>
char *common_char(char *string1, char *string2) {
int str_length1 = strlen(string1);
int str_length2 = strlen(string2);
char *new_string = malloc(str_length1+str_length2+1);
for (int index_1 = 0; index_1 < str_length1; index_1++) {
for (int index_2 = 0; index_2 < str_length2; index_2++) {
if (string1[index_1] == string2[index_2]) {
}
}
}
}
int main(void) {
return 0;
}
My idea is to find duplicate characters in string 2 and string 1 according to the nested loop, but there is a problem with the conditional statement, there is red line, also how to copy the character of the non-duplicate string?我的想法是根据嵌套循环查找字符串2和字符串1中的重复字符,但是条件语句有问题,有红线,还有如何复制非重复字符串的字符? I know strcopy(), but how to remove the repeated characters?我知道 strcopy(),但是如何删除重复的字符?
I've come up with a solution that uses dynamic memory and resizes the result char* each time a new char must be added.我提出了一个解决方案,它使用动态内存并在每次必须添加新字符时调整结果 char* 的大小。 There are two loops, the first iterates the b string and the second loop checks that non of char of the b string is repeated in the a string, if it is not repeated, then adds it.有两个循环,第一个迭代b字符串,第二个循环检查b字符串的非字符在a字符串中是否重复,如果不重复,则添加它。 Hope you understand the realloc to resize dynamically the char* each time it must be added an element.希望您了解 realloc 以在每次必须添加元素时动态调整 char* 的大小。
Firstly I initialize the result string to the size of string a so it can be all copied inside.首先,我将结果字符串初始化为字符串 a 的大小,以便可以将其全部复制到里面。 The ordering method I think it is called bubble method.我认为它的排序方法称为气泡法。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* common_char(char* a, char* b) {
char* result = (char*)malloc(sizeof(char)*strlen(a)+1);
int i = 0;
int j = 0;
int repeated = 0;
strcpy(result,a);
for(i=0; i<strlen(b); i++) {
for(j=0; j<strlen(result); j++) {
if(b[i] == a[j]) {
repeated = 1;
}
}
if(!repeated) {
result = (char*)realloc(result,strlen(result)+sizeof(char));
result[strlen(result)] = b[i];
result[strlen(result)+1] = '\0';
}
repeated = 0;
}
return result;
}
int main()
{
char a[] = "hello";
char b[] = "world";
char* result = common_char(a,b);
printf("%s", result);
return 0;
}
EDIT: I've modified the code to make it function.编辑:我已经修改了代码以使其正常工作。 About the comment of memory allocation, I've modified the declaration of result to give it space for the '\0'.关于内存分配的注释,我修改了结果的声明,给它空间'\0'。 When doing the realloc, I've already considered that the realloc does not increment the strlen() because strlen() is a counter till the '\0' not of the size of the variable.在执行 realloc 时,我已经考虑 realloc 不会增加 strlen() 因为 strlen() 是一个计数器,直到 '\0' 不是变量的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.