简体   繁体   English

在字符串中的特定字符之后添加字符(最多1个字符串,数组)

[英]Add character after specific character in string (max 1string,array)

I need to put * after every * in string 我需要在字符串中的每个*之后加上*

Input:*tret*ret*re*ter
Output:**tret**ret**re**ter

Well i have code that works but problem is that i can use only one string(array). 好吧,我有可以工作的代码,但是问题是我只能使用一个字符串(数组)。 And i used two. 我用了两个。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>

int main() {

    char x[256], y[256], z = 0;

    scanf("%s", x);

    for (char i = 0; i < strlen(x); i++) {

        y[z++] = x[i];

        if (x[i] == '*')
            y[z++] = '*';
    }

    y[z] = '\0';

    printf("%s\n", y);

    system("pause");
    return 0;
}

I will be thankful for any advice how to make it with only one string. 我将非常感谢您提供仅使用一个字符串的建议。

I even try but it makes soem chars disappear 我什至尝试,但它使炭黑消失了

int main() {

    char x[256], z = 0;

    scanf("%s", x);

    for (char i = 0; i < strlen(x); i++) {



        if (x[i] == '*') {
            x[++i] = '*';
        }
    }


    printf("%s\n",x);

    system("pause");
    return 0;
}

I will be thankful for any advice how to make it with only one string. 我将非常感谢您提供仅使用一个字符串的建议。

Algorithm 算法

Count the number of occurrences of '*' --> star_count . 计算'*' -> star_count的出现次数。

Note the length of the string --> length 注意字符串的长度-> length

Ensure length + star_count < 256, else buffer is too small. 确保length + star_count <256,否则缓冲区太小。

Loop from the end to the beginning. 从头到尾循环。

Set src to end of string. src设置为字符串的结尾。 (The null character) (空字符)

Set dest to end of src + star_count . dest设置为src + star_count

// *** // ***

Set *dest = null character. 设置* dest =空字符。

While not done ( src != beginning): 未完成时( src !=开始):
- Assign *dest = *src and decrement both pointers. -分配*dest = *src并递减两个指针。
- Conditionally assign *dest = '*' , decrement dest . -有条件地分配*dest = '*' ,减量dest


@Ian Abbott offers a nice improvement. @Ian Abbott提供了很好的改进。 Loop until pointers match. 循环直到指针匹配。

From *** 来自***

While dest > src 而目标> src
- Assign *dest = *src and decrement both pointers. -分配*dest = *src并递减两个指针。
- Conditionally assign *dest = '*' , decrement dest . -有条件地分配*dest = '*' ,减量dest

First of all, you should wonder why only one single array. 首先,您应该想知道为什么只有一个数组。 IMHO, if it is simpler with two arrays, stick there. 恕我直言,如果使用两个数组更简单,请粘贴在那里。

Next you use char as indexes. 接下来,使用char作为索引。 Please don't unless you have a strong reason to. 除非您有充分的理由,否则请不要这样做。 A decent compiler should warn you because it can be a cause of hard to detect errors. 一个好的编译器应该警告您,因为它可能导致难以检测到错误。 For example, if you implementation uses signed 8 bits char, the max value is 127 and not 255. So if the string contains 128 characters and the terminating null, the loop for (char i = 0; i < strlen(x); i++) { is an endless one. 例如,如果您的实现使用带符号的8位char,则最大值为127,而不是255。因此,如果字符串包含128个字符并且以终止符null表示,则for (char i = 0; i < strlen(x); i++) {的循环for (char i = 0; i < strlen(x); i++) {是无尽的。 Because 127+1 is likely to be -128 (undefined behaviour, but very common). 因为127 + 1可能是-128(不确定的行为,但很常见)。

And as you know the size of the x array, you should use a limited scanf and test its return value: 并且您知道x数组的大小,应该使用有限的scanf并测试其返回值:

if (scanf("%255s", x) != 1) {
    // process error
    ...
}

For the rest, @chux's answer gives you the only way to extend a string in place: 对于其他情况,@ chux的答案为您提供了就地扩展字符串的唯一方法:

  • scan it once to find its final size 扫描一次以找到其最终尺寸
  • process it starting from the end 从头开始处理

When you find an * character, you'll have to shift every byte after it to the right to make room for an additional * character. 当找到一个*字符时,必须将其后的每个字节向右移动,以腾出空间来容纳其他*字符。 Below is one such implementation: 以下是一种这样的实现:

#include<stdio.h>
#include <string.h>

int main(void)
{
    char x[256] = {'\0'}; // NUL Terminate Every Character

    printf("Enter String: ");
    scanf("%s", x);

    for(int i = 0; i < strlen(x); i++)
    {
        if(x[i] == '*')
        {
            for(int j = strlen(x) + 1; j > i; j--)
                x[j] = x[j - 1]; // Shift Every Byte, Starting With Last, To Right
            x[++i] = '*';
        }   
    }

    printf("%s\n", x);
    return 0;
}

Execution 执行

Enter String: *hi*buddy*
**hi**buddy**

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

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