在C编程中将元素插入到动态字符数组中

在C编程中将元素插入到动态字符数组中

本文介绍了在C编程中将元素插入到动态字符数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在C编程中添加元素到动态字符数组时,我有一些问题。以下是预期输出:

 您要输入的字符数:5 
输入字符串:datas
该字符串为:datas
要插入或2删除或3-quit?:1
要插入的字符是什么:a
生成的字符串:adata

我已经在主要功能中做了这些用户输入部分,这里是主要的代码我输入字符串输入,大小并传递给insert():

  printf(你想要多少个字符输入:); 
scanf(%d,& n);
str = malloc(n + 1);
printf(输入字符串类:);
scanf(%s,str);

case'1':
printf(你要插入的是什么字符);
scanf(%c,& input);
insert(str,input,n);
break;

而我的insert():



的部分

  void insert(char * str,char input,int n){
int i;
size_t space = 1; (i = 0; i str [i] =(char)(input + i);

space ++;
str = realloc(str,space);
if(i> 2){
break; (i = 0; i< n; i ++){
printf(%c,str [i]);
}
}


}
}

但是,当我尝试打印出来的字符串insert(),假设我输入'a'附加到动态数组的第一个元素,大小为5,我得到的结果是<$ c $我从,我不知道如何解决这个问题。感谢提前。

解决方案

这是代码 - 与合同,呼叫者做空闲的一点!调用者使用 insert(& str,input,n)调用它

  void insert(char ** str,char input,int n){

char * temp = * str;
int i;

* str = realloc(* str,n + 2); / * realloc first * /

if(!* str)/ * realloc failed * /
{
fputs(realloc failed,stderr);
free(temp); / *释放以前的malloced内存* /
exit(-1); / *退出程序* /
}

for(i = n; i> = 0; i--){
(* str)[i + 1] =(* str)[i]; / *将所有字符向上移动* /
}

(* str)[0] =输入; / *插入新的字符* /

printf(%s,* str); / *打印新的字符串* /
}

对于格式化很抱歉。这是留给读者的。我没有检查算法,但这并不会泄漏记忆


I was having some problem when trying to add element to dynamic char array in C programming. Here is the expected output:

How many characters do you want to input: 5
Input the string:datas
The string is: datas
Do you want to 1-insert or 2-remove or 3-quit?: 1
What is the character you want to insert: a
Resulting string: adata

I already did those user input part in the main function and here is the code in main where I take in the string input, size and pass them to insert():

printf("How many characters do you want to input: ");
scanf("%d", &n);
str = malloc(n + 1);
printf("Input the string class: ");
scanf("%s", str);

case '1':
    printf("What is the character you want to insert: ");
    scanf(" %c", &input);
    insert(str, input, n);
    break;

And the part where my insert():

void insert(char *str, char input, int n) {
int i;
size_t space = 1;
for (i = 0; i < n; i++) {
    str[i] = (char)(input + i);
    space++;
    str = realloc(str, space);
    if (i > 2) {
        break;
    }
}

for (i = 0; i < n; i++) {
    printf("%c", str[i]);
}
}

However, when I tried to print out the string from insert(), let's say I entered 'a' to append to the first element of dynamic array with a size of 5, the result that I am getting is abcd=

I referenced from the stackoverflow thread and I not sure how to fix this. Thanks in advance.

解决方案

Here is the code - with the contract that the caller does the free bit! The caller calls it with insert(&str, input, n)

void insert(char **str, char input, int n) {

char* temp = *str;
int i;

*str = realloc(*str, n + 2); /* realloc first */

if(!*str) /* realloc failed */
{
    fputs("realloc failed", stderr);
    free(temp); /* Free the previously malloc-ed memory */
    exit(-1); /* Exit the program */
}

for (i = n; i >= 0; i--) {
    (*str)[i + 1] = (*str)[i]; /* Move all characters up */
}

(*str)[0] = input; /* Insert the new character */

printf("%s", *str); /* Print the new string */
}

Sorry about the formatting. That is left to the reader. I have not checked the algorithm but this does not leak memory

这篇关于在C编程中将元素插入到动态字符数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:22