简体   繁体   English

将字符串中的一个字母复制到另一个字母中

[英]copy one letter from a string into another

So I need to create a function that copies the characters from the even position of one string into a dynamically create new string and then return the new string.所以我需要创建一个函数,将字符从一个字符串的偶数位置复制到一个动态创建的新字符串中,然后返回新字符串。 If I send the function "ABCDEFG" it returns "ACEG".如果我发送函数“ABCDEFG”,它返回“ACEG”。 How do you copy one character of a string into a new one in C?如何将字符串的一个字符复制到 C 中的一个新字符中? I tried the strcpy function, but it just printed the whole string.我尝试了 strcpy 函数,但它只是打印了整个字符串。

    char * str(char *s1){
    int i;
    char *s2;
    s2 = (char *)malloc(sizeof(char ));
    for(i = 0; i < strlen(s1); i++){
        if(i % 2 == 0){
            ///copy from s1 to s2
            printf("s2: %s", s2);   
        }
    }
    return s2;
    }

Use strcat() function available in C. You don't need to copy char from s1 to s2 instead append the required characters as string from s1 to string s2.使用 C 中可用的strcat()函数。您不需要将 char 从 s1 复制到 s2,而是将所需的字符作为字符串从 s1 附加到字符串 s2。 This is enough hint to solve your issue.这足以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM