将字符串插入C中的字符串 [英] Inserting String into String in C

查看:114
本文介绍了将字符串插入C中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长度功能:

int length(const char s[])
{
    int length = 0;
    while(s[length]!='\0')
    {
        length++;
    }

    return length;
}

插入功能:

    void insert(char s1[], const char s2[], int n)
{
    char *beginOfString = s1;
    int lengthOfS1 = length(s1);
    int lengthOfS2 = length(s2);
    char s1Copy[lengthOfS1 + lengthOfS2];
    int c, afterC, lengthOfRemainingPart;

    c = 0; 
    c = n + 1;
    beginOfString += c;
    afterC = c; //nth position to start from to add onto array
    lengthOfRemainingPart = length(beginOfString);
    c = 0;

    int counter = 0;
    for(c = 0; c < length(s1) - 1; c++) {

        s1Copy[c] = s2[counter];
        for(c = afterC; c < (lengthOfS1 + lengthOfS2) - 1; c++) {
            if(c == afterC) {
                s1Copy[c] = s2[counter];
            } else {
                counter++;
                if(s2[counter] != *"\0")
                s1Copy[c] = s2[counter];
            }
        }
    }
    c = 0;

    for(c = 0; c < length(s1Copy) - 1; c++) {
        printf("\n %c \n", s1Copy[c]);
    }

    printf("\n");
    printf("\n %s \n", beginOfString);
    printf("\n  %s \n", "LINE");
}

函数调用(和相关的声明):

Function Call (and related declarations):

#define MAX_STR 20
char ab[MAX_STR + 1] = "Chicken and Chips";
   char  b[MAX_STR + 1] = "Scampi";       
   insert(ab, b, 7);

我正在尝试将一个char数组插入另一个char数组,同时将其余char保留在该数组中,但是根据用户希望根据n值插入char数组的位置而移动.

I'm trying to insert a char array into another char array whilst keeping the rest of the chars still within the array but shifted along depending on where the user wants to insert the char array according to the n value.

这似乎不起作用,并且似乎输出了错误的值.函数调用和函数标头(参数类型等)需要保持我的放置方式不变.只有功能体本身可以更改.

This doesn't seem to be working and seems to output the wrong values. The function calls and the function header (parameter types, etc.) needs to stay how I've put them. Only the function body itself can change.

输出应为"ChickenScampi and Chips"

The output should be "ChickenScampi and Chips"

有什么想法我要去哪里吗?干杯.

Any ideas where I am going wrong? Cheers.

推荐答案

不准备加糖.这段代码是一团糟.如果您只是简单地尝试完成的任务是最简单的

Not going to sugar coat it. This code is a mess. The task you're trying to accomplish is easiest if you simply

  • 从[c0>从[0..min(n,lengthS1))(注意右侧的排他性)复制到目标.
  • s2[]附加到目标.
  • s1[min(n, lengthS1)]s1[lengthS1]附加到目标.
  • 终止字符串
  • 就是这样.
  • Copy from [0..min(n, lengthS1)) (note exclusivity of right side) from s1[] to your target.
  • Append s2[] to the target.
  • Append s1[min(n, lengthS1)] through s1[lengthS1] to the target.
  • Terminate the string
  • That's it.

并且最重要的是,目标必须能够容纳两个字符串一个nulchar终止符,而目标缓冲区则不能这样做(短1个字符)

And above all, the target must be able to accommodate both strings and a nulchar terminator, which your target buffer does not do (it is short by one character).

不需要嵌套的for循环.而且,无论您使用的是自己的索引变量,都是损坏的.

There is no need for nested for-loops. And yours is broken regardless as it is walking on its own indexing variables.

#include <stdio.h>

size_t length(const char s[])
{
    const char *end = s;
    while (*end)
        ++end;
    return end - s;
}

void insert(const char s1[], const char s2[], int n)
{
    size_t lengthOfS1 = length(s1);
    size_t lengthOfS2 = length(s2);
    size_t pos = (n < lengthOfS1) ? n : lengthOfS1;
    size_t i;

    // declare VLA for holding both strings + terminator
    char s1Copy[lengthOfS1 + lengthOfS2 + 1];

    // put in part/full s1, depending on length
    for (i=0; i<pos; ++i)
        s1Copy[i] = s1[i];

    // append s2
    for (i=0; i<lengthOfS2; ++i)
        s1Copy[pos+i] = s2[i];

    // finish s1 if needed.
    for (i=pos; i<lengthOfS1; ++i)
        s1Copy[i+lengthOfS2] = s1[i];

    // termiante string
    s1Copy[lengthOfS1 + lengthOfS2] = 0;

    puts(s1Copy);
}

int main()
{
    char ab[] = "Chicken and Chips";
    char  b[] = "Scampi";
    insert(ab, b, 0);
    insert(ab, b, 7);
    insert(ab, b, 100);
}

输出

ScampiChicken and Chips
ChickenScampi and Chips
Chicken and ChipsScampi

摘要

如果您不确定发生了什么,那么您应该做的 last 就是编写 more 代码.相反,停止编码,获取一些论文和书写工具,然后重新考虑问题.

If you're not sure what is going on, the last thing you should be doing is writing more code. Rather, stop coding, get some paper and a writing instrument, and rethink the problem.

这篇关于将字符串插入C中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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